Cin >> a >> '/' >> b;

Hello I'm quite new to C++ (Actually moved to C++ from C recently)
So, I was trying to write a C++ program where I intended to read a rational number as p/q so I wrote something like "Cin >> a >> '/' >> 'b;" But it doesn't works and says something like Ambiguous Overload detected.
Any Help?
Last edited on
1
2
3
4
5
float a,b,c;

std::cin >> a;
std::cin >> b;
c = a/b;


it is advisable to read a beginner book of C++ to get the understanding of syntax...
@AnkitSingh 12
Hello, I think you misinterpreted my question. I was not trying to calculate but rather read a rational number as numerator and denumerator separately. In C I used to do it as,

scanf("%d/%d",&a,&b);

Just don't know how to do it in C++.
Thanks in advance.
I'm not sure you can get it specifically to have p/q but here's what I would do;

1
2
3
4
5
6
7
8
cout << "Enter number 1" << endl;
cin >> FirstNumber;

cout << "Enter number 2" << endl;
cin >> SecondNumber;

cout << "Numerator = " << FirstNumber << endl;
cout << "Denominater = " << SecondNumber << endl;


Hope it helps somewhat
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
#include <iostream>
using namespace std;

int main () {

int a, b, sum  = 0;
char myChar;


cout<<"Enter first number: ";
cin>>a;

cout<<"Enter / for this operation; "
cin>>myChar;

cout<<"Enter second number: ";
cin>>b;

switch (myChar) {

case '/'": 

if (b == 0){

cout<<"Error"<<endl;
}
else

sum = a/b;
cout<<"The sum is: "<<sum<<endl;
break;

default: cout<<"Error! You entered wrong operation!"<<endl;
break;

}

cin.get(); cin.get();
return 0;
 



This code is not compiler. You may get some error. :))
@ALL:
Well thanks for suggestion guys, but I don't want other alternatives because I already know them, I was just asking whether it is possible to do it that way or not. If you guys know if its possible then do write but now I'm starting to think that it is not. Anyways, thanks for help. And do post if you know the method. THANKS!!
You can use your C method in C++

Just add "#include<cstio.h>" (I think that's right) and your old code should still work.
Last edited on
Topic archived. No new replies allowed.