|
 |
|
 |
05-21-2004, 02:58 PM
|
#1 (permalink)
|
|
Registered User
Join Date: May 2004
Posts: 4
|
getline-command not working properly???
Hi, I'm new in this forum, and I have been programming in C++ for about ½ a year or so... I am currently developing a console game, in which I wish for the player to be able to write a nick that may include spaces (whitespaces)... So I looked through the net to find a solution to my problem, as this cannot be done with the standard cin-command... I stumbled upon the getline(cin, string) command, and used it in my code; however, it seems to screw up everything. I am using the Microsoft Visual C++ compiler, and the code I am trying to get to work looks like this:
Code:
header("\t\t\tRelentless Faith - Role-Playing Game\n\n\t\t\tCharacter creation - Identity");
write("Before you may start the game you have to create you
character.\nThis will only take a few moments, remember, that you
can at any time in the game type \"help\", \"exit\" or \"restart\".
\n\nNow just follow the instructions point by point.\n\nFirst of
all, you need to decide what you will be known as during the
game.\n\nWrite the name you wish to be known by here (may not
contain any spaces): \n",8);
getline(cin, charnick);
write("\nYou will henceforth be known as: ",15);
write(charnick,15);
write("\n\nNow to the next step in creating your character...",15);
anykey();
gender();
I've included the necessary libraries, and defined the string variable earlier in the code. Don't mind the write()- and header() functions, they are simply just outputs of the text defined in the first parameter... So, my problem is, as mentioned, the getline function, that just won't seem to work properly... If you have any suggestions I'd be very thankful :)...
Last edited by ~Relentless~; 05-22-2004 at 02:03 PM.
|
|
|
05-21-2004, 06:44 PM
|
#2 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Code:
#include <string>
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
string nick;
cout<<"Enter your nick: ";
getline(cin, nick);
cout<<nick<<endl;
return 0;
}
Or:
Code:
int main(int argc, char* argv[])
{
char nick[12];
cout<<"Enter your nick: ";
cin.getline(nick, 12, '\n' );
cout<<nick<<endl;
return 0;
}
Observe the cin.getline versus getline.
The cin version is the istream version. Use this when you work with C-style strings. Use getline without "cin" for the string type.
__________________
|
|
|
05-22-2004, 09:21 AM
|
#3 (permalink)
|
|
Registered User
Join Date: May 2004
Posts: 4
|
Okay, thanks for your suggestions... However, if I use the first of your suggestons, my problem remains intact - you have to push enter twice in the programme, for it to actually set the string...
In case number 2, the programm, with a few modifications, works perfectly, so I thank you very much for the help  ...
|
|
|
05-22-2004, 09:58 AM
|
#4 (permalink)
|
|
Registered User
Join Date: May 2004
Posts: 4
|
Hmm, it seems I rushed a bit into song there... Apparently there's one problem with the code...:
Code:
#include <iostream>
#include <string>
using namespace std;
char choicechar[1000];
string charnick;
void main()
{
cout << "Enter nick: ";
cin >> choicechar[1000];
cin.getline(choicechar, 1000, '\n');
charnick = choicechar;
cout << endl << endl << charnick;
}
The output will be like this:
Enter nick: bjørn mikkelsen
jørn mikkelsen
What obscure reason causes this effect? Should it not be able to read the full line of the input? Thanks in advance...
|
|
|
05-22-2004, 10:51 AM
|
#5 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Remove:
Code:
cin >> choicechar[1000];
That I didn't present in my code.
What this line does is reading the first character from the stream. So that character is "eaten" away.
Then when you exectute:
Code:
cin.getline(choicechar, 1000, '\n');
The first character (wich was "B") is gone. Therefore cin.getline will read what is left of the stream.
I have a few tips, though wich go beyond your question:
- You preserved 1000 bytes (almost 1 Kilo Bytes) for a nick.
1000 characters is about 12 lines of characters for a nick in a console app. Console is 81 characters wide.
- Why declaring a C-style char (choicechar[1000]) only to store it in a C++ style string (charnick)?
Use my first code with getline.
- You declared your variables on a global scope. Better forget that that's possible for now.
Once you are ready for (multi)threaded applications you will find out why. Declare them in main, or even better,
the class that handles user input.
- You are implementing main() with a void return type.
Do it like this:
Code:
int main()
{
// your code here
return 0;
}
This has to do with portability with other compilers.
- Look at the second line of your code on top of this thread.
One long sentence or so. Split it over multiple lines.
Code:
int main()
{
string charnick;
cout << "Before you may start the game you have to create you character.\n"
"This will only take a few moments, remember, "
"that you can at any time in the game type \"help\", "
"\"exit\" or \"restart\". \n\nNow just follow the instructions point by point."
"\n\nFirst of all, you need to decide what you will be known as during the game."
"\n\nWrite the name you wish to be known by here (may not contain any spaces): \n";
getline(cin, charnick);
cout << endl << endl << charnick<<endl;
return 0;
}
This is much more readable, on forums and during coding.
__________________
Last edited by Valmont; 05-22-2004 at 11:17 AM.
|
|
|
05-22-2004, 02:01 PM
|
#6 (permalink)
|
|
Registered User
Join Date: May 2004
Posts: 4
|
Okay, thanks for the advices... And here returns the first problem... the getline-command screws up my input-order somehow... (The reason I use global variablesm is that the user only uses the main function in a very little timeperiod, as the main function calls upon another function)... But my problem with the getline command is, that if I create a program with only that piece of code, as you've suggested, the console application needs the user to push enter twice, before accepting the input, which I find extremely annoying, and I am sure that some of the players who will (hopefully  ) play my game will be confused by this... So is there any chance you have a tip on how to eliminate this error?
|
|
|
05-22-2004, 06:06 PM
|
#7 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Show me the code, then I'll look into it, because I don't understand like this.
Or send it to valmont_programming@hotmail.com .
__________________
|
|
|
07-13-2004, 01:20 PM
|
#8 (permalink)
|
|
Registered User
Join Date: Jul 2004
Posts: 1
|
Quote:
Originally posted by ~Relentless~
...the console application needs the user to push enter twice, before accepting the input, which I find extremely annoying...
|
Not sure if this helps, but see if you have any cin.ignore's following the call to getline. If so, remove them. That solved this problem for me.
|
|
|
08-03-2004, 10:56 AM
|
#9 (permalink)
|
|
Registered User
Join Date: Aug 2004
Posts: 2
|
Code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string charnick = ""; //changed this line
cout << "Before you may start the game you have to create you character.\n"
"This will only take a few moments, remember, "
"that you can at any time in the game type \"help\", "
"\"exit\" or \"restart\". \n\nNow just follow the instructions point by point."
"\n\nFirst of all, you need to decide what you will be known as during the game."
"\n\nWrite the name you wish to be known by here (may not contain any spaces): \n";
getline(cin, charnick);
cout << endl << endl << "You will henceforth be known as: " << charnick << endl;
return 0;
}
i didn't read all the posts, but i just took a look at your code. i'm not sure if this helps, but i changed the part where you are calling charnick variable ( string charnick; ) to ( string charnick = "";
|
|
|
10-11-2004, 05:38 PM
|
#10 (permalink)
|
|
Registered User
Join Date: Oct 2004
Posts: 1
|
The "hit enter twice" is because the string library for VC++ 6 contains a design problem. It also places a null string in the input buffer. Go to http://support.microsoft.com/default...;en-us;q240015
for more information.
You should try to fix the problem:
Edit line 165 of the file:
c:\Program Files\Microsoft Visual Studio\VC98\Include\STRING
Line 165 should be:
_I.rdbuf()->sbumpc();
instead of:
_I.rdbuf()->snextc();
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 09:30 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|