Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 15th, 2006, 11:14 PM   #1
b1g4L
Programmer
 
Join Date: Dec 2005
Location: SC
Posts: 38
Rep Power: 0 b1g4L is on a distinguished road
Send a message via AIM to b1g4L
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;
and the user types in (without the quotes) "a b c d e f g h"
How can I get each char separately? I need to do some processing on each char.
b1g4L is offline   Reply With Quote
Old Feb 16th, 2006, 12:36 AM   #2
jaeusm
Programmer
 
jaeusm's Avatar
 
Join Date: Feb 2006
Location: Columbus, OH
Posts: 84
Rep Power: 3 jaeusm is on a distinguished road
char c;
cin.get(c);
This does not ignore spaces, however.

You could also read in the whole line and parse the string.
jaeusm is offline   Reply With Quote
Old Feb 16th, 2006, 8:00 AM   #3
xpy
Newbie
 
Join Date: Feb 2006
Posts: 1
Rep Power: 0 xpy is on a distinguished road
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)...
xpy is offline   Reply With Quote
Old Feb 16th, 2006, 9:49 AM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Feb 16th, 2006, 9:59 AM   #5
b1g4L
Programmer
 
Join Date: Dec 2005
Location: SC
Posts: 38
Rep Power: 0 b1g4L is on a distinguished road
Send a message via AIM to b1g4L
Quote:
Originally Posted by DaWei
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.
Any advice on how to input a char at a time of a string?
b1g4L is offline   Reply With Quote
Old Feb 16th, 2006, 10:36 AM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Sure. If you declare a single character,
   char c;
   cin >> c;
cin will only go for one character, instead of a possible array of char, and not overflow. Of course you have to dispose of 'c' somewhere before you fill it again (as with a loop). There are several methods associated with cin that allow you to read in different types, C++ strings, char arrays with a limit specified, etc. The extraction operator (>>) is not the only method associated with cin. I recommend you consult your documentation regarding cin and istreams.
__________________
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
DaWei is offline   Reply With Quote
Old Feb 16th, 2006, 1:05 PM   #7
b1g4L
Programmer
 
Join Date: Dec 2005
Location: SC
Posts: 38
Rep Power: 0 b1g4L is on a distinguished road
Send a message via AIM to b1g4L
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;
}
b1g4L is offline   Reply With Quote
Old Feb 16th, 2006, 1:16 PM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Feb 16th, 2006, 2:30 PM   #9
ivan
Professional Programmer
 
ivan's Avatar
 
Join Date: Sep 2005
Location: serbia & montenegro
Posts: 484
Rep Power: 4 ivan is on a distinguished road
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.
ivan is offline   Reply With Quote
Old Feb 16th, 2006, 6:58 PM   #10
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
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.
InfoGeek is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:58 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC