-5

so, i have this code. i have some questions about it, im at the beggining of learning c++.
My questions are:

how can i read more than one name in scanf ?

or when i want to read a street name [strada], eg: 1 northwood 34/6, when i insert space it passes the line and gets on next line, what i need to change in order to work as i want?

and why i cant print my person when i initialise ?

thanks

//create a struct which represent a person and that person must contain 
//firstname lastname age, and adress. adress must contain street city and 
//country 

//define a function which it will be used for reading a person from keyboard. //define a function which will initialise a person.
//create a function which it will be used for releasing allocated resources for a person.
//create a functon which it will be used for printing the data about a person
// read from the keyboard a collection of persons, arrange those persons decreasing by age using qsort.

#include "stdafx.h"
#include <malloc.h>
#include <stdlib.h>
#include <string>
using namespace std;

struct adresa{char* strada, *oras,*judet;};
struct persoana{char* nume, *prenume;int varsta;adresa adr; };

void trimNewline(char* str)
{
int len = strlen(str);
if (str[len - 1] == '\n')
{
    str[len - 1] = '\0';
}
}
void citestePersoana(persoana* pers){
  printf("Nume:");
  scanf("%s", pers->nume);
  printf("Prenume:");
  scanf("%s", pers->prenume);
  printf("Varsta:");
  scanf("%d", pers->varsta);
}

void citesteAdresa(adresa* adr){
printf("Strada:");
fgets(adr->strada, 100, stdin);
trimNewline(adr->strada);

printf("Oras:");
fgets(adr->oras, 100, stdin);
trimNewline(adr->oras);

printf("Judet:");
fgets(adr->judet, 100, stdin);
trimNewline(adr->judet);
}

void initPersoana(persoana* pers){
  pers->nume = (char*)malloc(100 * sizeof(char));
  pers->prenume = (char*)malloc(100 * sizeof(char));
  pers->varsta = (int)malloc(100 * sizeof(int));
}

void initAdresa(adresa* adr){
  adr->strada = (char*)malloc(100 * sizeof(char));
  adr->oras = (char*)malloc(100 * sizeof(char));
  adr->judet =(char*)malloc(100 * sizeof(char));
}

void afispers(){
  persoana x;
  adresa y;
  printf("Nume:%s", x.nume);
  printf("Prenume:%s", x.prenume);
  printf("Varsta:%s", x.varsta);
  printf("Strada:%s", y.strada);printf("Oras:%s", y.oras);
  printf("Judet:%s", y.judet);
}

int _tmain(int argc, _TCHAR* argv[]){
  persoana persoanaCurenta;
  adresa adresaCurenta;
  initPersoana(&persoanaCurenta);
  initAdresa(&adresaCurenta);
  citestePersoana(&persoanaCurenta);
  citesteAdresa(&adresaCurenta);
  afispers();
  return 0;
}

1 Answers1

0

If you want to read the address with whitespaces, such as

1 northwood 34/6

into one string, it's better to use fgets instead of scanf.

fgets(adr->strada, 100, stdin);

and then trim the newline character from it.

int len = strlen(adr->strada);
if ( adr->strada[len-1] == '\n' )
{
   adr->strada[len-1] = '\0';
}

You can put that code in a function and re-use it for other data you read.

void trimNewline(char* str)
{
   int len = strlen(str);
   if ( str[len-1] == '\n' )
   {
      str[len-1] = '\0';
   }
}

and then use it as:

void citesteAdresa(adresa* adr){
  printf("Strada:");
  fgets(adr->strada, 100, stdin);
  trimNewline(adr->strada);

  printf("Oras:");
  fgets(adr->oras, 100, stdin);
  trimNewline(adr->oras);

  printf("Judet:");
  fgets(adr->judet, 100, stdin);
  trimNewline(adr->judet);
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • I can use the void for the name as well ? what about printing whole person datas, should i use printf ? – Sabău Adrian Dec 01 '15 at 17:12
  • I edited the code as you explained to me, but when it reaches first fgets it skips to second scanf, and i dont know where is the problem, i updated the code. can you explain to me where im wrong? @R Sahu – Sabău Adrian Dec 01 '15 at 17:31
  • @SabăuAdrian, editing your code that much is a big NO. When you do that, answers that were based on the old code don't make sense any more. – R Sahu Dec 01 '15 at 17:33
  • @SabăuAdrian, I understand whey it appears to skip the first `fgets`. What 's really happening is that the newline character from the previous `scanf` is left in the input stream and the first call to `fgets` reads just that newline and proceeds. You'll need to add a line that reads and discards all the characters up to and including the newline before calling `fgets`. – R Sahu Dec 01 '15 at 17:36
  • See http://stackoverflow.com/a/31168220/434551 for how to ignore up to and including a newline. – R Sahu Dec 01 '15 at 17:38
  • Thanks for advises @R Sahu. – Sabău Adrian Dec 01 '15 at 17:54