Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5428
Number of posts: 16949

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Appending user inputs to string arrays... Posted by ADizzle491 on 16 Feb 2009 at 9:22 PM
Hi all, currently I need to write a program that asks the user to type names (once) and as they type names and hit enter, each name gets put into a string array. When the input is "DONE", the program moves on and stops writing to the name array (ignoring DONE as a name obviously).

My question is, how would I go about coding the loop to do this? I've tried a few whiles but I keep getting errors regarding comparing cin and "DONE". I've never had to record inputs like this, I'm used to inputs such as "name name name name name DONE" then break it up into elements, this however just operates as "name enter, name enter, name enter, DONE enter etc."

How would I go about forming the loop for this?

PS I also need to update a counter variable to keep track of the number of people/elements in the array, however that shouldn't be an issue for me to incorporate within the loop.
Report
Re: Appending user inputs to string arrays... Posted by virtual_arts on 18 Feb 2009 at 5:05 AM
try the following code:-

int acceptNames(){
  char names[][15]; //max 14 characters per name
  char tmpname[15];
  int i = 0, done = 0; // set flags and initialize counter
  do{
    printf("\nName %d : ",i+1);
    scanf(" %[^\n]",tmpname); // alternatively use: gets(tmpname);
    if(strcmp(tmpname,"DONE") == 0){// if "DONE"
      done = 1;
    }
    else{
      strcpy(names[i],tmpname);// copy name to array
    }
    ++i; // increment counter
  }while(!done);// repeat while done != 0
  return(i); // return the number of names read
}

Report
Re: Appending user inputs to string arrays... Posted by Lundin on 18 Feb 2009 at 6:49 AM
: char names[][15]; //max 14 characters per name

You don't allocate any memory for the strings. You need to allocate some before this line:

: strcpy(names[i],tmpname);// copy name to array

Or the program will crash and/or behave randomly.
Report
Re: Appending user inputs to string arrays... Posted by Malcolm_McLean on 18 Feb 2009 at 10:26 AM
And if you are going to use gets() at all, pass it a buffer of at least 1024 characters. Otherwise it is easy to crash your program by typing too many characters at the commandline. Then use strlen() to check for overlong entries.
Report
Re: Appending user inputs to string arrays... Posted by Lundin on 19 Feb 2009 at 3:36 AM
scanf() has the same problem. gets() and scanf() are both to be considered debug / hobby functions.

http://c-faq.com/stdio/scanfprobs.html
Report
Re: Appending user inputs to string arrays... Posted by virtual_arts on 19 Feb 2009 at 4:53 AM
hey thanks guys!! those were valuable pieces of information.. :)



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.