-
program problems
ok i want the user to enter a name and have the program save it to a array
Code:
void NewGame()
{
system("clear");
cout<<"what would you like to have your name as?"<<endl;
cin.getline(Name,10,' ');
cout<<Name;
game();
}
but were i have ' ' i had to put that to get it to do anything... when i didnt have it no matter what i pressed it didnt move any farther in the program it just stuck at entering input... after i put the ' ' i have to put a space at the end of the name for it to work... so i though why dont i put '\n' there so when they press enter it would keep going but that didnt work either.... and it doesnt work when i dont have anything there either...
thx for any help
-
hmm '\n' should work just fine, but why are you using getline anyways? is Name a string or an array?
if Name is indeed a string just have cin>>Name;
also Name is neither passed into this function or declared inside of it...
edit:: err, sorry I ahven't used getline in such a long time that I forgot it wont work with a string at all. :(
-
So the following code doesn't pass the getline() call in the following code for you?
Code:
int main()
{
char Name[10];
cout<<"what would you like to have your name as?"<<endl;
cin.getline(Name,10);
cout<<Name;
return 0;
}
-
Name is an array of 20 objects (Name[20])
ok i changed it to just cin>>Name; but that doesnt get me any farther... now the problem is it wont do anything after i enter a name and press enter... it just goes to the next line (does it make any difference that im using linux??) were i can type more info. here is the code
Code:
void NewGame()
{
system("clear"); //would clrscr() work?
cout<<"what would you like to have your name as?"<<endl;
cin.getline(Name,10, ' ');
cout<<Name;
game();
}
...
void game()
{
cout<<"this far";
if (maplvl != 1);
{
cout<<"im here";
if (maplvl == 2)
{
fin.open("map2.txt", ios::app);
while(!fin.eof());
{
cout<<"im here2";
fin >> szMap[x][y];
cout<<szMap[x][y]<<endl;
x++;
}
mapLoad = true;
}
}
}
im still working with the file input output stuff (its not working either) so the game() function is far from complete.. im just working on getting the program to get that far.
thx
-
Using an array of strings to hold names:
Code:
#include <iostream>
int main(void)
{
std::string s[2];
for (int i = 0; i < 2; i++)
{
std::cout <<"Enter name " <<i <<":" <<std::endl;
std::getline (std::cin, s[i]); // Do error checking !
}
for (int i = 0; i < 2; i++)
{
std::cout <<"Name " <<i <<" is " <<s[i] <<std::endl;
}
return(0);
}
-
thx for the code but isnt it easier to just put
using namespace std;
instead of std:: infront of everything?
-
ok this is what i got now
Code:
void NewGame()
{
system("clear");
cout<<"what would you like to have your name as?"<<endl;
getline(cin,Name);
cout<<Name;
game();
}
...
but it still doesnt make it to the game() function it just stalls at the input part... whatever i put nothing happens
-
>>but isnt it easier to just put
>>using namespace std;
Whatever .... ;)
http://faq.cprogramming.com/cgi-bin/...&id=1043284351
-
>> it just stalls at the input part
Post something that we can compile to allow us to see your problem fully.
Code:
#include <iostream>
using namespace std;
void game(void)
{
cout << "Game run" << endl;
}
void NewGame(void)
{
string Name;
system("clear");
cout << "what would you like to have your name as?" << endl;
getline(cin, Name);
cout << Name;
game();
}
int main(void)
{
NewGame();
}
-
yes that is basicaly it but it doesnt work on my computer.. if i use cin.getline(Name, 19) it just skips over getting the user input... but if i use cin.getline(Name, 19, ' ') it waits till i enter a name then a space and then prints out what i entered then stopps.
im starting to get really fustrated...
well here is the whole source (very rough and uncomplete)
-
Code:
void game()
{
cout<<"this far";
if (maplvl != 1);
Extra ; at the end of that last line?
-
well that fixed alot but also brought more problems :( lol
ok now i get to the points were output is "this far" "im here" and "im here2" but im here 2 loops untill there is a segmentation fault. which is good cause im progressing :D
ok now for the next question: do you know of any good tutorials on file i/o and storing a file that looks like
xxxx
xxxx
xxxx
xxxx
into an array? because what i got isnt working
-
So, I changed :
>char Name[20];
to be
>string Name;
and
>cin.getline(Name, 19,' ');
to be
>getline(cin, Name);
Code:
what would you like to have your name as?
hammer
hammer
this farim here
>>fin.open("map2.txt", ios::app);
Get that worked before using it.
>>while(!fin.eof());
Another extra ; character.
Plus, read the FAQ here:
http://faq.cprogramming.com/cgi-bin/...&id=1043284351