• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C++: Have cin use another character besides whitespace as variable separator...

Superwormy

Golden Member
What I ultimately want to do is this:

int Coefficient;
int Exponent;
cout << "Enter the polynomial: ";
while (cin >> Coefficient >> Exponent)
; // do something

So normally, 'cin' knows they are seperate variables by whitespace inbetween, so it'd be find if I did this:

3 2


But what I want to do is this:
3x^2

And have it still understand that 3 is the Coefficient and 2 is the Exponent. Basically treat the x^ as whitespace...

...how?
 
Why not just read it in as a string and parse it from there? You may be able to do what you're suggesting, but parsing a string will be a more robust & flexible solution.
 
use the character search to look for the x^ characters.

You can define any character you wish for your psuedo token.

Then extract the numbers and manipulate as desired.
 
Why do any of that?

Just do a cin>>c>>t>>tt>>e;

where t and tt are just some random char t,tt;

All you have to do after that is validate that (t == 'x' || t=='X') && (tt='^')

 
Originally posted by: MCrusty
Why do any of that?

Just do a cin>>c>>t>>tt>>e;

where t and tt are just some random char t,tt;

All you have to do after that is validate that (t == 'x' || t=='X') && (tt='^')

Because if you enter bad input - like a character where a number is expected, it will crash your input stream. Try entering qx^3 instead of 2x^3 and see what you get out of your program. You can test for this and reset the stream state of course, but it's better to start with a string IMO.
 
Originally posted by: Superwormy
...can you give me a starting point then to break up the string and convert it to doubles/integers?
Your going to need to do some lexical parsing. There's lots of good classes out there to do this for you, but you can do it yourself too.

Basically what you do is dump all of your input in to a string and then go about validating it. After you have validated it, then you read the parts you need from the string. Heres a simpleminded way to go about doing it that should give you a good starting point. This uses a bit of STL. It doesn't do any string validating.
 
Originally posted by: Superwormy
What I ultimately want to do is this:

int Coefficient;
int Exponent;
cout << "Enter the polynomial: ";
while (cin >> Coefficient >> Exponent)
; // do something

So normally, 'cin' knows they are seperate variables by whitespace inbetween, so it'd be find if I did this:

3 2

Start reading about string manipulation functions. It's easy, bbut I'm going to make you do the work to figure it out.
But what I want to do is this:
3x^2

And have it still understand that 3 is the Coefficient and 2 is the Exponent. Basically treat the x^ as whitespace...

...how?

 
Back
Top