How to put a full sentence into an array?

Reply

Join Date: Oct 2009
Posts: 1
Reputation: RobertMW is an unknown quantity at this point 
Solved Threads: 0
RobertMW RobertMW is offline Offline
Newbie Poster

How to put a full sentence into an array?

 
0
  #1
33 Days Ago
I am trying to write a program that will input a bunch of names into an array of structures under the .name heading. Currently I have what is below to get it to work. But it's not very flexible. You have to input both a first and a last name before it will go on. Is there any command similar to cin.getline for C++ that will work in this instance? Or a way to leave last as null if a second ("last") name isnt input that time around?

  1. for (count_s=0; count_s < num_stu; count_s++){
  2. printf("\nEnter name of student #%i: ",count_s+1);
  3. scanf("%s %s", &first, &last);
  4. strcpy(student[count_s].name, first)
  5. strcat(student[count_s].name, " ");
  6. strcat(student[count_s].name, last);
  7.  
  8. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 362
Reputation: gerard4143 is on a distinguished road 
Solved Threads: 46
gerard4143's Avatar
gerard4143 gerard4143 is offline Offline
Posting Whiz
 
0
  #2
33 Days Ago
Originally Posted by RobertMW View Post
I am trying to write a program that will input a bunch of names into an array of structures under the .name heading. Currently I have what is below to get it to work. But it's not very flexible. You have to input both a first and a last name before it will go on. Is there any command similar to cin.getline for C++ that will work in this instance? Or a way to leave last as null if a second ("last") name isnt input that time around?

  1. for (count_s=0; count_s < num_stu; count_s++){
  2. printf("\nEnter name of student #%i: ",count_s+1);
  3. scanf("%s %s", &first, &last);
  4. strcpy(student[count_s].name, first)
  5. strcat(student[count_s].name, " ");
  6. strcat(student[count_s].name, last);
  7.  
  8. }
Do you mean like C's getline function?
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,033
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic
 
1
  #3
33 Days Ago
  1. fgets( student[count_s].name, sizeof (student[count_s].name), stdin );
Reply With Quote Quick reply to this message  
Reply

Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC