GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 14-Oct-2006, 16:29
MSUracer MSUracer is offline
New Member
 
Join Date: Oct 2006
Posts: 19
MSUracer is on a distinguished road

Storing strings in an array...


got another one for you guys...

i need to take up to 15 names from the user, and store them in an array. here's what i have so far...

CPP / C++ / C Code:
#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main()
{
    string name;
    int numnames = 0;
    int max = 14;
    string totnames[max];
    
    cout << "Enter a name.  Enter 'finished' to stop." << endl;
    cin >> name;
    while ((name != "finished") && (numnames < max))
    {
          cout << "Enter another name. Enter 'finished' to stop."<< endl; 
          name = totnames[numnames];  
          numnames++;
          cin >> name;        
    }
    
    
    cout << "You have entered the following names:" << endl;
    cout << name << endl;
    
    system("pause");


}
it will accept up to 15 names, and will stop accepting names when "finished" is entered, but it will only print out "finished" or the last name i entered. im pretty sure it's a small mistake, but i can't find it.

thanks in advance.
ryan
  #2  
Old 14-Oct-2006, 16:30
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

Re: Storing strings in an array...


It looks like the code you have to save the name is
CPP / C++ / C Code:
name = totnames[numnames];
This is backwards. You want
CPP / C++ / C Code:
totnames[numnames] = name;
By the way, some compilers (those that adhere most strictly to the standard) won't let you (or at least will issue a warning telling you not to) declare an array with variable dimensions. By that I mean the number 14 has to be a #define'd constant.
  #3  
Old 14-Oct-2006, 16:39
MSUracer MSUracer is offline
New Member
 
Join Date: Oct 2006
Posts: 19
MSUracer is on a distinguished road

Re: Storing strings in an array...


thanks, ill give that a shot. kudos to a quick response, too.
  #4  
Old 14-Oct-2006, 16:45
MSUracer MSUracer is offline
New Member
 
Join Date: Oct 2006
Posts: 19
MSUracer is on a distinguished road

Re: Storing strings in an array...


hmm, i switched them around, same result. i played around with my cout statement some...

if i use
CPP / C++ / C Code:
cout << "You have entered the following names:" << endl;
    cout << name << endl;
then i get "finished" or the last name entered.

if i use
CPP / C++ / C Code:
cout << "You have entered the following names:" << endl;
    cout << totnames[numnames] << endl;
then i get black space displayed.

what do i want to use in that cout statement?

if i use
CPP / C++ / C Code:
cout << "You have entered the following names:" << endl;
    cout << numnames << endl;
then i get the number of names i entered. i think my loop works, im just asking for the wrong information from it?
  #5  
Old 14-Oct-2006, 17:08
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

Re: Storing strings in an array...


It's hard to guess what you've done without seeing all of the new code. The key is to make sure you store the newly inputted string in an array before overwriting the variable.
  #6  
Old 14-Oct-2006, 17:32
MSUracer MSUracer is offline
New Member
 
Join Date: Oct 2006
Posts: 19
MSUracer is on a distinguished road

Re: Storing strings in an array...


CPP / C++ / C Code:
#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main()
{
    string name;
    int numnames = 0;
    int max = 14;
    string totnames[max];
    
    cout << "Enter a name.  Enter 'finished' to stop." << endl;
    cin >> name;
    while ((name != "finished") && (numnames < max))
    {
          cout << "Enter another name. Enter 'finished' to stop."<< endl; 
          totnames[numnames] = name;  
          numnames++;
          cin >> name;        
    }
    
    int a;
    cout << "You have entered the following names:" << endl;
    for (a = 0; a < numnames; a++)
        cout << name << endl;
    
    system("pause");


}

i'm a little closer with this one. i forgot that i need a loop to print out all the names stored. added the loop, and the program now prints out the correct number of names, but it prints out either "finished" or the last name entered for each entry.

ex:
if the user inputs "red, green, blue," then "finished," i get "finished, finished, finished," for my output.
any ideas?
  #7  
Old 14-Oct-2006, 18:42
Sokar Sokar is offline
Member
 
Join Date: May 2005
Posts: 243
Sokar has a spectacular aura aboutSokar has a spectacular aura about

Re: Storing strings in an array...


What you posted will not compile for me. I get the following errors.

error C2057: expected constant expression (int max = 14;)
error C2466: cannot allocate an array of constant size 0 (string totnames[max];)
error C2133: 'totnames' : unknown size (string totnames[max];)
error C2059: syntax error : '<<' (cout << << endl;)

But with fixing those errors I get this output.

Enter a name. Enter 'finished' to stop.
red, green, blue," then finished
Enter another name. Enter 'finished' to stop.
Enter another name. Enter 'finished' to stop.
Enter another name. Enter 'finished' to stop.
Enter another name. Enter 'finished' to stop.
You have entered the following names:
red,
green,
blue,"
then
  #8  
Old 14-Oct-2006, 18:56
MSUracer MSUracer is offline
New Member
 
Join Date: Oct 2006
Posts: 19
MSUracer is on a distinguished road

Re: Storing strings in an array...


fixed it, sorry. i forgot to put the "name" back in the cout statement, hence the two <<'s. i appreciate the help, but i dont know what those errors mean. care to explain?
  #9  
Old 14-Oct-2006, 19:07
Sokar Sokar is offline
Member
 
Join Date: May 2005
Posts: 243
Sokar has a spectacular aura aboutSokar has a spectacular aura about

Re: Storing strings in an array...


Quote:
Originally Posted by MSUracer
i appreciate the help, but i dont know what those errors mean. care to explain?
Sure. I will not debate this with other members. But "max" should be a constant.
http://msdn.microsoft.com/library/de...html/C2057.asp

Where did you store the names(in what variable)?
  #10  
Old 14-Oct-2006, 19:12
MSUracer MSUracer is offline
New Member
 
Join Date: Oct 2006
Posts: 19
MSUracer is on a distinguished road

Re: Storing strings in an array...


im attempting to store the names entered into the variable "name," and then put the different "name"s into an array, that i can search (that comes later).
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to clear the strings in the array? jpxtreme C Programming Language 9 29-Sep-2006 07:41
Need help deleting the last element in the array headphone69 C++ Forum 2 15-Mar-2006 20:31
Declaring an array of strings dynamically (C Language) Hyuga C Programming Language 5 03-Aug-2005 10:18
template comiling problems - need expert debugger! crq C++ Forum 1 01-Feb-2005 22:26
array of pointers to strings mirizar C++ Forum 5 21-Jan-2005 11:24

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 13:39.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.