What am i doing wrong here?

I just wanted to try to use the if statement in a program so I though of putting 4 different calculating programs into one and the user would have the choice of picking which one he wants to use.Except that regardless if he enters the correct character or not, it always displays the if statement which is used for displaying an error.

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

using namespace std;

int main ()
{
    char a,b,c,d;
    char A = a; //In case the user enters A instead of a the program won't work correctly.
    char B = b; //In case the user enters B instead of b the program won't work correctly.
    char C = c; //In case the user enters C instead of c the program won't work correctly.
    char D = d; //In case the user enters D instead of d the program won't work correctly.

    cout << "Welcome to a collection of four programs." << endl;
    cout << "To choose one of the programs, enter one of the letters: A, B, C or D." << endl;
    cout << "The letter A will start program 1." << endl;
    cout << "The letter B will start program 2." << endl;
    cout << "The letter C will start program 3." << endl;
    cout << "The letter D will start program 4." << endl;
    cout << "" << endl;
    cout << "1.Program for calculating the area of a square." << endl;
    cout << "2.Program for calculating perimeter of a circle ." << endl;
    cout << "3.Program for calculating the ratio of two numbers." << endl;
    cout << "4.Program for calculating the product of two numbers ." << endl;
    cin >> A;
    if( a != a || a != b || a != c || a != d)
    {
        cout << "Error: You didn't enter any of the letters: A, B, C or D." << endl;
        return 0;
    }
    if(a == A || a == a)
    {
        int x;

        cout << "You chose the program for calculating an area of a square." << endl;
        cout << "Enter a value for the side of the square:" << endl;
        cin >> x;
        cout << "The area of a square with a=" << x << " is " << x*x << "." << endl;
    }

    return 0;
}


Of course, i haven't finished it but i stopped at the error part and can't seem to solve it, i think its something with the != part, which is not equal.
Last edited on
closed account (48T7M4Gy)
I think you mean 'a' the character where you have a the variable name in line 25 etc

Also you should initialize the variables in line 7

line 30 looks grim too
cin >> A points to A which points to a.
so i think its the same, can you edit the code and point out the errors i cant seem to understand?
Edit: i see, except that when i tried to make it a character it gives me a build error so i left it a, i cant use "a"
Last edited on
When you run your program the compiler runs into your uninitialized char variables which lead to undefined behaviors.

1
2
3
4
5
char a, b, c, d;
a = ' ';
b = ' ';
c = ' ';
d = ' ';
I dont have to set a value for a, only for A.
A = a
regardless if they enter a or A it should go with the value for a
Except that i dont get whats wrong here
Edit: Hold on, trying to edit what kemort said
Edit: Nope, still get the same error message which should only be printed when they dont enter a b c d A B C or D.
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
#include <iostream>
#include <cstdlib>

using namespace std;

int main ()
{
    char a,b,c,d;
    char A = 'A';
    char B = 'B';
    char C = 'C';
    char D = 'D';


    cout << "Welcome to a collection of four programs." << endl;
    cout << "To choose one of the programs, enter one of the letters: A, B, C or D." << endl;
    cout << "The letter A will start program 1." << endl;
    cout << "The letter B will start program 2." << endl;
    cout << "The letter C will start program 3." << endl;
    cout << "The letter D will start program 4." << endl;
    cout << "" << endl;
    cout << "1.Program for calculating the area of a square." << endl;
    cout << "2.Program for calculating perimeter of a circle ." << endl;
    cout << "3.Program for calculating the ratio of two numbers." << endl;
    cout << "4.Program for calculating the product of two numbers ." << endl;
    cin >> a;
    if(A != 'a' || A != 'b' || A != 'c' || A != 'd')
    {
        cout << "Error: You didn't enter any of the letters: A, B, C or D." << endl;
        return 0;
    }
    if(A == A || A == a)
    {
        int x;

        cout << "You chose the program for calculating an area of a square." << endl;
        cout << "Enter a value for the side of the square:" << endl;
        cin >> x;
        cout << "The area of a square with a=" << x << " is " << x*x << "." << endl;
    }

    return 0;
}
Last edited on
closed account (48T7M4Gy)
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
#include <iostream>

using namespace std;

int main()
{
	char response = ' ';

	cout << "Welcome to a collection of four programs." << endl;
	cout << "To choose one of the programs, enter one of the letters: A, B, C or D." << endl;
	cout << "The letter A will start program 1." << endl;
	cout << "The letter B will start program 2." << endl;
	cout << "The letter C will start program 3." << endl;
	cout << "The letter D will start program 4." << endl;
	cout << "" << endl;
	cout << "1.Program for calculating the area of a square." << endl;
	cout << "2.Program for calculating perimeter of a circle ." << endl;
	cout << "3.Program for calculating the ratio of two numbers." << endl;
	cout << "4.Program for calculating the product of two numbers ." << endl;

	cin >> response;
	response = toupper(response);             // convert to uppercase
	cout << "Response: " << response << endl; // just to check
	
	if (response != 'A' && response != 'B' && response != 'C' && response != 'D')
	{
		cout << "Error: You didn't enter any of the letters: A, B, C or D." << endl;
		//return 0;                           // leave out for a while
	}

	if (response == 'A')
	{
		int x;

		cout << "You chose the program for calculating an area of a square." << endl;
		cout << "Enter a value for the side of the square:" << endl;
		cin >> x;
		cout << "The area of a square with a=" << x << " is " << x*x << "." << endl;
	}
       
        return 0;
}


Try this way. You don't have to convert to uppercase but it helps.

Hopefully you can see why I used response as a variable name - it's clearer what's going on and your upper and lowercase 'alphabet soup' of variable names and the mixing with their values disappears. :)
Last edited on
closed account (48T7M4Gy)
I dont have to set a value for a, only for A.
A = a

No you don't have to but it is poor programming practice not to initialize variables as they are created. By doing it you at least have some control over what happens rather than just having weird junk values appearing.

Also A=a is not intializing A. You are assigning a to A and a is not initialized so if a has a junk value out of your control, so then has A.
One more question:
response = toupper(response); // convert to uppercase

Regardless if someone enters a or b or ... it will automatically convert that value to an uppercase one?
And I just removed stdio.h and the program doesnt compile with errors :/
Thanks for the help tho, had a lot of mistakes there
closed account (48T7M4Gy)
Regardless if someone enters a or b or ... it will automatically convert that value to an uppercase one?

Yes

"And I just removed stdio.h and the program doesnt compile with errors :/"
I'll check - I hope my dream hasn't been shattered.
closed account (48T7M4Gy)
Yeah, you're right because toupper() is also part of iostream or someone will come along with a more esoteric reason. Well done.

It should have been <cctype> and not <stdio.h> anyway as it turns out. I misread the tutorial reference at http://www.cplusplus.com/reference/cctype/toupper/ such is life, my dream will repair quickly.
Last edited on
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>

using namespace std;

int main()
{
	char letter = ' ';

	cout << "Welcome to a collection of four programs." << endl;
	cout << "To choose one of the programs, enter one of the letters: A, B, C or D." << endl;
	cout << "The letter A will start program 1." << endl;
	cout << "The letter B will start program 2." << endl;
	cout << "The letter C will start program 3." << endl;
	cout << "The letter D will start program 4." << endl;
	cout << "" << endl;
	cout << "1.Program for calculating the area of a square." << endl;
	cout << "2.Program for calculating the perimeter of a circle ." << endl;
	cout << "3.Program for calculating the ratio of two numbers." << endl;
	cout << "4.Program for calculating the product of two numbers ." << endl;

	cin >> letter;
	letter = toupper(letter);

	if (letter != 'A' && letter != 'B' && letter != 'C' && letter != 'D')
	{
		cout << "Error: You didn't enter any of the letters: A, B, C or D." << endl;
	}
	if (letter == 'A')
	{
		int side;

		cout << "You chose the program for calculating an area of a square." << endl;
		cout << "Enter a value for the side of the square:" << endl;
		cin >> side;
		cout << "The area of a square with a=" << side << " is " << side*side << "." << endl;
	}
	if (letter == 'B')
	{
        float radius;
        float L = 2*radius*3.14; //weird error again

        cout << "You chose the program for calculating the perimeter of a circle." << endl;
        cout << "Enter a value for the radius of the circle:" << endl;
        cin >> radius;
        cout << "The perimeter of a circle with a radius of " << radius << " is " << 2*radius*3.14 << "." << endl;
	}
	if (letter == 'C')
    {
        float first;
        float second;

        cout << "You chose the program for calculating the ratio of two numbers." << endl;
        cout << "Enter a value for the first number:" << endl;
        cin >> first;
        cout << "The value of the first number is " << first << ".Enter a value for the second number:" << endl;
        cin >> second;
        cout << "The ratio between the numbers " << first << " and " << second << " is " << first/second << "." << endl;
    }
    if (letter == 'D')
    {
        float first;
        float second;
        float third = first*second; //weird error

        cout << "You chose the program for calculating the product of two numbers." << endl;
        cout << "Enter the value of the first number:" << endl;
        cin >> first;
        cout << "The value of the first number is " << first << ".Enter a value for the second number:" << endl;
        cin >> second;
        cout << "The product of the numbers " << first << " and " << second << " is " << first*second << "." << endl;
    }
	return 0;
}


So far, one problem. I will enter more ifs between the ifs to display errors if needed, but for example

{
float first;
float second;
float third = first*second; //weird error

cout << "You chose the program for calculating the product of two numbers." << endl;
cout << "Enter the value of the first number:" << endl;
cin >> first;
cout << "The value of the first number is " << first << ".Enter a value for the second number:" << endl;
cin >> second;
cout << "The product of the numbers " << first << " and " << second << " is " << third << "." << endl;
}
This prints out incorrect information. Instead i have to use << first*second << Any other ways to avoid this and define it so that it can be faster?

Edit:Heres another interesting thing
If i enter a lowercase letter it wont print an error or anything, thats why i made a = A, or tried to at least, i wanted to make any lowercase letters which are entered to take the value of the same letter, except uppercase.

Edit: if (letter == 'A' || letter == 'a')
Fixes the problem, ill just edit the rest of the code
Last edited on
closed account (48T7M4Gy)
One thing before I take a closer look is for you to consider the ideal control for your menu - i.e. a switch which is a couple of screens into the following tutorial:

http://www.cplusplus.com/doc/tutorial/control/

Take particular notice of how the default: works
closed account (48T7M4Gy)
This prints out incorrect information. Instead i have to use << first*second << Any other ways to avoid this and define it so that it can be faster?
1
2
3
float first;
float second;
float third = first*second; //weird error 

...
cout << "The product of the numbers " << first << " and " << second << " is " << third << "." << endl;

This is almost the classic reason why you must initialise variables
Last edited on
closed account (48T7M4Gy)
First you need
1
2
3
float first = 0;
float second = 0;
float third = first*second; //No longer a weird error, just zero 

You would be better off with
float third = 0; and leaving the calculation to later on.

Second,
cout << "The product of the numbers " << first << " and " << second << " is " << third << "." << endl; This is not what you want because it will just print zero if you initialise as I suggest.

And that is not what you want because you have to calculate third after you input the values. So you need
1
2
3
4
cin >> first
cin >> second etc
third = first * second
cout blah bla << third;




Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if(response == 'A')
{
  ...
}
else if (response == 'B')
{
  ...
}
else if (response == 'C')
{
  ...
} etc
else
{
   cout << "Wrong response";
   etc
}
closed account (48T7M4Gy)
If i enter a lowercase letter it wont print an error or anything, that's why i made a = A, or tried to at least, i wanted to make any lowercase letters which are entered to take the value of the same letter, except uppercase.


That's all up to you and how you plan your project and its operation. The toupper() is not mandatory just a common failsafe for sloppy user data entry/capslock positions etc. toupper() is the reason why lowercase is accepted. It converts the response to uppercase!
Last edited on
@OP if you've learned about function prototypes/function headers you can avoid all of these if/else godawful messes. Also, as @kemort mentioned, a switch statement coupled with a toupper() or tolower() function would go great with you menu.

As for what I mentioned, function prototyping and especially important scoping of variables makes your code more organized and you'll be able to avoid garbage value errors (most of the time).

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

using namespace std; // don't do this. I only did because of the length of the example.

float multiply( float &x, float &y );
// add your other functions here

int main( void )
{
    // write whatever std::cout statements you need
    float x;
    float y;
    
    cout << "Enter first value: ";
    cin >> x;
    cout << "Your first value was " << x << ".";
    cout << "Enter your second value: ";
    cin >> y;
    
    cout << "The product of the numbers, " << x << " and " 
         << y << " are " << multiply(x,y) << endl;
    
    return 0;
}

float multiply( float &x, float &y )
{
    // if you want std::cout and std::cin statements in your functions instead of main(), 
    // go for it, but for ease of reading code and debugging, you shouldn't
    // input -> implementation -> output
    return x*y;
}
Well so far far we have been learjijg about cout and cin in school I just wante to try something else, thats why I didnt know how :P
Thanks for the help tho, I can mark this as solved
Topic archived. No new replies allowed.