cin.putback

I have a problem. how to use cin.putback.
For example, i write a sentence "add 10 to 3"
and i want to use cin.putback to find "add" and two number.
and it should come out like this 10 + 3 = 13

My code

cout << "\n\t\tAction: ";
cin.get(ch2);
cin.putback(ch2);
cin >> ch1;
cin >> num1 >> num2;
if ( ch2 == 'a' || ch2 == 'A'){
cout << num1 << " + " << num2 << " = " << num1+num2;}

how to find those to numbers.
ty
Don't use putback(). It is a bad solution. (And it won't work for what you are trying to do.)

Just parse the line directly:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>
using namespace std;

void complain( const string& message )
  {
  cin.clear();
  cin.ignore( 1000, '\n' );
  cout << "Expected input: " << message << endl;
  }

void add()
  {
  int num1, num2;
  string _to_;
  cin >> num1 >> _to_ >> num2;

  if (!cin) { complain( "add N to M" ); return; }

  cout << num1 << " + " << num2 << " = " << (num1 + num2) << endl;
  }

void subtract()
  {
  int num1, num2;
  string _from_;
  cin >> num1 >> _from_ >> num2;

  if (!cin) { complain( "subtract N from M" ); return; }

  cout << num2 << " - " << num1 << " = " << (num2 - num1) << endl;
  }

int main()
  {
  while (true)
    {
    cout << "Action: " << flush;
    string action;
    cin >> action;

    if      (action == "add")      add();
    else if (action == "subtract") subtract();
    else if (action == "quit")     break;
    else cout << "What?\n";
    }
  return 0;
  }

You might want to use some functions like the following to convert to lowercase or before comparing the strings:
http://www.cplusplus.com/forum/beginner/11304/page1.html#msg53368
Here are the three functions uppercase(), lowercase(), and titlecase() in a single module for you:
http://www.cplusplus.com/forum/beginner/15368/page1.html#msg76089

You may also want to check only the significant letters. You can use string::find() to see if the thing you want is found at position zero.
if (lowercase( action ).find( "sub" ) == 0) subtract();

You may also want to add in an option to give the user "help", etc.

Good luck!
sry i had been busy these days. I must use cin.putback and switch case in this program
I did some part of program.
The only problem in this is i cant type too many letter
for example "add 2 to 4" it will go wrong. it will work with "a 2 t 3".
This is my code

#include <iostream>
using namespace std;

int main() {

double num1,num2;
char ch2,ch3;

cout << "\n\t\tAction: ";
cin.get(ch2);
cin.putback(ch2);
cin >> num1;
cin >> ch3;
cin >> num2;

if ( ch2 == 'a' || ch2 == 'A') {
cout << num1 << " + " << num2 << " = " << num1+num2;

else if ( ch1 == 's' || ch1 == 'S')
cout << num1 << " - " << num2 << " = " << num1-num2;
else if ( ch1 == 'm' || ch1 == 'M')
cout << num1 << " * " << num2 << " = " << num1*num2;
else if ( ch1 == 'd' || ch1 == 'D')
cout << num1 << " / " << num2 << " = " << num1/num2;
// else if ( ch1 == 'r' || ch1 == 'R')
// cout << num1 << " ^ " << num2 << "=" << pow(num1,num2);
else
cout << "I do not know how to perform this action.\n";
}
return 0;
}


ty
Well, tell whomever wrote your requirements that you cannot use putback() to do this, because it won't work.

Either that or you have not told me something about your requirements.
This is my first semester.

there is no other requirements in the program. but it has to include switch case and putback and peek for this project.

no files nor functions

this is the book i use STARTING OUT W/C++,FROM
CONTROL...-W/CD By GADDIS 6th
Last edited on
Well I haven't looked at your code too closely, and I'm very tired, but from the looks of it--I'm also not 100% sure what you're trying to do here, I need a more thorough explanation and I think there is a much simpler way using the cin.putback function to accomplish what you're doing. As far as what I'm making an assumption that this is one problem of the few, scroll down:

You have code stating:
1
2
3
4
5
6
7
char ch2,ch3;

cout << "\n\t\tAction: ";
cin.get(ch2);
cin.putback(ch2);
cin >> num1;
cin >> ch3;


You said that ""add 2 to 4" it will go wrong. it will work with "a 2 t 3"."
That is because type char only holds one character, so it will stop after the first character.
You first need to fix that, so instead of char put string. At the top of your program you must also put #include <string> to include the string class.

Hope that helped, if you have further questions and want to solve this quickly toss me your Skype or AIM username and I will help.

Last edited on
[hint] Perhaps if you were to post your exact homework assignment [/hint]
Topic archived. No new replies allowed.