How to ignore certain things entered into console?

Hello, I'm new here and you'll be hearing from me a lot! Ive recently gotten into C++ and I decided to make a program to add two fractions. But what Ive gotten setup only accepts input like "1 2 3 4", but I want it to accept "1/2 3/4", how would I go about doing it?

here is my code

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
#include <iostream>


int main()
{
	int iFracNum1 = 0;
	int iFracNum2 = 0;
	int iFracDen1 = 0;
	int iFracDen2 = 0;
	int iFracNum3 = 0;
	int iFracDen3 = 0;

	std::cout << "Please enter the fractions" << std::endl;
	std::cin >> iFracNum1 >> iFracDen1 >> iFracNum2 >> iFracDen2;



	iFracNum3 = iFracNum1*iFracDen2 + iFracNum2*iFracDen1;
	iFracDen3 = iFracDen1*iFracDen2;

	std::cout << iFracNum3 << "/" << iFracDen3 << std::endl;
	iFracNum3 = 0;
	iFracDen3 = 0;


	getchar();
	getchar();

	return 0;

}	


thank you very very much, I'll sorry if this is a n0ob question, thank you.
Do you want it to accept both "1 2 3 4" and "1/2 3/4" or just the latter?

If just the latter, basically just provide a dummy variable to discard the slashes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Example program
#include <iostream>

int main()
{
    int a;
    int b;
    int c;
    int d;
    
    char temp_slash;

    std::cin >> a >> temp_slash >> b >> c >> temp_slash >> d;
    
    std::cout << "you entered: " << a << "/" << b << " " << c << "/" << d << std::endl;
}


2/3   4 /    5
you entered: 2/3 4/5
Last edited on
@Ganado Thank you! Is having a fake variable the only way? It seems a bit unprofessional. I'm more focused on writing good code than code that barely works. Not that youre version is bad, I'm just curious of there is a more professional way.

Thank you again. Have a nice day
Yeah, I understand. But I can't think of a better way for something so simple, which I think the issue here, because the slash doesn't actually provide any function in your program. It's simply there as part of your required syntax.

You could do a safety check to make sure the character put into temp_clash is actually a slash, but other than that, I'm not sure what else there really is to do.

Kinda requires knowing what direction you want to take the program in. If you allowed more than one type of operator (ex: "3/4 5*6") then the slash would no longer be useless, because you'd actually be using it as part of the logic to determine whether you should divide or multiply, for example.

______________________________________

Just for fun, added some minor error checking:

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>

class Fraction {
  public:
    Fraction()
    : numerator(0), denominator(1) {}
    
    Fraction(int numerator, int denominator)
    : numerator(numerator), denominator(denominator) {}
    
    
    double getValue() const 
    {
        return static_cast<double>(numerator) / denominator;   
    }
    
    int numerator;
    int denominator;

};

std::istream& operator>>(std::istream& is, Fraction& frac)
{
    char slash;
    is >> frac.numerator >> slash >> frac.denominator;
    if (!is || slash != '/')
        throw "Exception occurred while parsing user input to fraction";
    return is;
}

int main()
{
    Fraction p;
    Fraction q;
    
    try {
        std::cout << "Enter first fraction: ";
        std::cin >> p;
        std::cout << "Enter second fraction: ";
        std::cin >> q;
        
        std::cout << p.getValue() << " " << q.getValue() << std::endl;
    }
    catch (const char* msg) {
        std::cout << msg << std::endl;
    }
}


Enter first fraction: 4/ 6
Enter second fraction: 11 / 100
0.666667 0.11
Last edited on
IMO, it makes more sense to set the stream's failure state rather than throwing an exception from the formatted input function. This makes the function's behavior consistent with the built-in ones, which obey the stream's exception mask.

1
2
3
4
5
6
7
std::istream& operator>>(std::istream& is, Fraction& frac)
{
    char slash;
    is >> frac.numerator >> slash >> frac.denominator;
    if (is && slash != '/') is.setstate(std::ios::failbit);
    return is; 
}


Absolutely agree, I had not thought of that functionality.
Thank you guys so much! Sorry for late reply, Have a great weekend!
Topic archived. No new replies allowed.