How to put a full sentence into an array?

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
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
Oct 29th, 2009
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: 394
Reputation: gerard4143 is on a distinguished road 
Solved Threads: 47
gerard4143's Avatar
gerard4143 gerard4143 is online now Online
Posting Whiz
 
0
  #2
Oct 30th, 2009
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,042
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: 178
Aia's Avatar
Aia Aia is offline Offline
Postaholic
 
1
  #3
Oct 30th, 2009
  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



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC