C++ List

Pages: 12
Thanks for the help guys
Last edited on
sample program here and tutorial
http://cplusplus.com/doc/tutorial/dynamic/
Thanks for helping. I tried to use that but I really don't understand it, I tried to change the wording around to make it so you can put how many games you have and and what the names are. I'm having lots of problems with it.
have you tried the sample program? all you have to do is change int to string.
I tried changing to string and the wording around it didnt run nor work.
Last edited on
the original thing i was trying is
#include <iostream>
#include <string>

using std::cin; using std::endl;
using std::cout; using std::string;

int main()

{
// ask how many favorite games the person has
cout << "Please enter how many favorite games you have(max 6): ";

// read how many favorite games
string games;
cin >> games;

// ask for the names of the games
cout << "Please enter your favorite games: ";

cin >> games;

// Read the names of each game back


}
I don't know what to do...i need to have this done before 4pm EST. I just cannot figure it out..
int main()

{
// ask how many favorite games the person has
cout << "Please enter how many favorite games you have(max 6): ";

// read how many favorite games
string games[6];
int max = 0;


// ask for the names of the games
while(max != 6)
{cout << "Please enter your favorite games: ";

cin >> games[max];
max=max+1;}


// Read the names of each game back

max = 0;

while(max !=6)
{cout << max << ": " << games[max] << endl;
max=max+1;}

}

I never compiled this so there are probably errors but you shouldn't procrastinate so we are both in the wrong. I also didn't do the whole #include thing since what you have will probably do.
Last edited on
How do you use it for words in stead of numbers blackcoder41?
Hey kid what you have is right, the games variable should be read in as a string instead of an int or in other words, "words in stead of numbers". The only thing you did wrong is not include the loop, not use an array to store the titles or a number to hold your place in the array. You seem to have missed reading a chapter or two in your studies but I procrastinated worse when I was young and never even thought to go to a forum.
I'm having troubles figuring out how to use a loop for it..
That's the while(max!=6){...max=max+1;} that I have in the code. Literally while max is_not_equal to 6 do_this...after_this max=max+1;. The while statement is your loop. I have a second while loop processing the reading back of the list because I am what we call lazy, and I do not want to type all of that crap out. Since we know what the limit of the array is going to be we can hardcode it and simplify the condition that keeps the while loop going. Notice that I reset the max counter in between the two while loops, this is so that we can use the same int variable for the same purpose in a differant loop.
the only problem is when i space the names out of the games they will go in seperate columns
and no matter how many numbers i put in from 1 - 6 for how many games it gives me the 5-6 spaces anyways...any ideas whats going on? lol
Yeah that's done on purpose. The endl means output new line (or carridge return).
^Just an FYI, std::endl means a newline, as well flushing the buffer. So if you just want to put a newline, just use '\n'.
and no matter how many numbers i put in from 1 - 6 for how many games it gives me the 5-6 spaces anyways...any ideas whats going on? lol

use getline(cin, games[max]); instead of cin >> games[max];

more info here:
http://cplusplus.com/reference/string/getline/
well now today I need to do the same thing but I need to use vectors. how would I do that?
The endl means output new line (or carridge return).

Carriage return just returns the cursor to the 0 position on the x axis. That's why windows does CRLF ('\r\n'): it's carriage return and then linefeed to take you to x = 0, y += 1. I have to admit, the UNIX way of having LF implicitly CR'ing doesn't make sense... but it does save diskspace, even if not much...

Edit: anyway, like firedraco said, std::endl does a CRLF and then flushes the buffer (equivalent would be std::cout << "\n" << std::flush;), so use '\n' unless you want the buffer flushed (personally, I tend to use and std::endl before asking for input, and after printing several lines of output, as well as before exiting from the program).
Last edited on
Pages: 12