![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
|
cin to getnext
I need a way to get next token after I input a list of chars using spaces to separate them.
For instance, if I input a string like this: string tempInput; cin >> tempInput; How can I get each char separately? I need to do some processing on each char. |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Feb 2006
Location: Columbus, OH
Posts: 84
Rep Power: 3
![]() |
char c; cin.get(c); You could also read in the whole line and parse the string. |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Feb 2006
Posts: 1
Rep Power: 0
![]() |
this will help you...
use this....
char c[]=""; cin>>c; now you can either use it like this: cout<<c; //prints the whole word or like this: cout<<c[3]; //prints the fouth letter of the word you dont need know the length of the word c++ sets automaticaly the last letter as char(0)... |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
That's shitty advice. Please don't do that. It will not help, it will hinder. 'char c [] = "";' sets aside an array of char of only one byte, the terminator. Attempting to put any input into it will overflow and cause any number of problems, most likely including a segmentation fault.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#5 | |
|
Programmer
|
Quote:
|
|
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Sure. If you declare a single character,
char c; cin >> c;
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#7 |
|
Programmer
|
Ok this will input a string until whitespace is ecountered or end of string. I need to be able to ignore whitespace.....any ideas?
#include <iostream>
using namespace std;
int main ()
{
string tempInput;
cout << ("Enter a string") << endl;
cin >> tempInput;
cout << ("String entered was: ") + tempInput << endl;
cout << ("Chars in order entered") << endl;
for (int i = 0; i < tempInput.length(); i++)
{
cout << tempInput[i] << endl;
}
return 0;
} |
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Read an entire line. That's why I suggested other cin methods. "getline" is one. It allows you to read an entire line (you specify the terminator, if you like), or up to some maximum number of characters you specify (to avoid overflow), whichever comes first. Whether you use the extraction operator or another method, don't forget to test for failure. Your user can easily cause it. Failing to check is amateurish at best and dereliction once you know you need to.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#9 |
|
Professional Programmer
Join Date: Sep 2005
Location: serbia & montenegro
Posts: 484
Rep Power: 4
![]() |
If all letters will be separate by a white space you could read the entire line and than parse the line with strtok() function. I will leave you to read the documentation about the function.
|
|
|
|
|
|
#10 |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
strtok works with char arrays only. See this thread for a C++ way: http://www.programmingforums.org/for...ead.php?t=5037
__________________
PFO - My daily dose of technology. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|