Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   need help! problems on strings.. REPLY ASAP (http://www.go4expert.com/forums/help-strings-reply-asap-t22806/)

Khiine 25Jul2010 15:44

need help! problems on strings.. REPLY ASAP
 
i can't input a string after entering an integer! why is that?..

Khiine 25Jul2010 15:49

Re: need help! problems on strings.. REPLY ASAP
 
i crashes.. example when i enter an integer using scanf, then after that, when i try to enter a string using gets it crashes.. NEED HELP ON THIS PLS!

Ancient Dragon 25Jul2010 19:54

Re: need help! problems on strings.. REPLY ASAP
 
The reason you can not enter a string after entering an integer is because when you type the digits of the integer then press the Enter key the program leaves the Enter key in the keyboard. You have to remove that '\n' Enter key byte from the keyboard buffer after getting the integer. You did not say whether you are using C or C++ so I can not tell you how to do that. Post some code that shows us how you got the integer.

Ancient Dragon 25Jul2010 19:59

Re: need help! problems on strings.. REPLY ASAP
 
>> i try to enter a string using gets
Oh, I suppose I should have read your post more thoroughly. You are using C language. Then just call getchar() after getting input integer.

About gets() -- NEVER EVER use it because it will allow you to enter a string larger than the amout of space the input buffer can hold, and that will most likely crash your program. Always use fgets() instead of gets(). But that, too, as a drawback in that it may put the '\n' at the end of the string, which you will want to strip out before using the string for anything.

An alternative is to call scanf() like this: Assume you want the user to enter 10 characters
Code:

char name[11];
printf("Enter your name (up to 10 characters)\n");
scanf("%10s", name);


With the above if you enter more than 10 characters scanf() will leave the remaining characters in the keyboard buffer.

xpi0t0s 26Jul2010 16:15

Re: need help! problems on strings.. REPLY ASAP
 
scanf is a really pants way of getting input from the user. Use fgets instead (using stdin as the file descriptor). Then parse out the stuff you want from that string.

The \n at the end of the fgets result is useful: it tells you whether you got the whole input or not. If you asked for 80 characters and the user entered less than that, you get the input + \n. If they entered more than that you get the first 79 characters of input and a \0, and the remaining input can be retrieved with another call to fgets. Always worth watching out for that cos if you expect the complete input after a call to fgets and they typed too much, this can throw the rest of your program off.

LordN3mrod 8Sep2010 02:10

Re: need help! problems on strings.. REPLY ASAP
 
Since everybody has answered in C, I'll answer in C++.
If you write
Code:

#include <iostream>
#include <string>
int main()
{
  using namespace std;
  int n;
  string s;
  cin >> n;
  cin >> s
}

then everything is fine, nothing will crash and you will read the next "string" until any spaces or newlines. That is, if the input were

4
bubu gaga

then n would be 4, and s would be "bubu".

However, if you need to read the whole next line (with a getline) you'll first have to "eat" the rest of the current line. Like this

Code:

#include <iostream>
#include <string>
int main()
{
  using namespace std;
  int n;
  string s;
  cin >> n;
  getline(cin, s); //eat the end of the line into s;
  getline(cin, s); //actually read the next line into s;
}

Best Regards,
Armen

virxen 8Sep2010 03:30

Re: need help! problems on strings.. REPLY ASAP
 
if you insist on using scanf then after each scanf add one getchar();


All times are GMT +5.5. The time now is 15:18.