My first C++ Prog

well im not new to programming(java) but new to c++

i need this program to read 3 integers ( they cannot be the same) from a user and compute the highest and smallest number, then compute if each number is even or odd.

for some reason the compiler tells me that the middle maxNum isnt initialized.
i dont understand that part.

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
#include <iostream>
#include <string>
using namespace std;

int main()
{
 int userNumOne;
 int userNumTwo;
 int userNumThree;
 int maxNum;
 int minNum;
 string again;

 cout << " this program will ask you to input three integers\n"
	  << " please dont do not use a number more than once.\n";
 do
 {

 cout << "please enter your first number. \n"; 
 cin >> userNumOne;

 cout << "please enter your second number. \n";
 cin >> userNumTwo; 

 if ( userNumTwo != userNumOne)
 {
    cout << "please enter your third number. \n";
    cin >> userNumThree;
 }


if ((userNumThree != userNumTwo) && (userNumThree != userNumOne))
{   
    userNumOne == maxNum;

	if (userNumOne < userNumTwo)
	{	userNumTwo = maxNum;
	}

	if ( maxNum < userNumThree)
	{	userNumThree = maxNum;
    } 
    
	userNumOne == minNum;

	if ( userNumOne > userNumTwo)
	{	userNumTwo = minNum;
    }
	if ( minNum > userNumThree)
	{	userNumThree = minNum;	
    }
	if (userNumOne % 2 == 0)
	{	cout << " your first number is an even number\n";
    }
	if (userNumTwo % 2 == 0)
	{	cout << " your second number is an even number\n";
    }
	if (userNumThree % 2 == 0)
	{	cout << " your third number is an even number\n";
    }
}

 cout << "Do you want to try again (y or n)?";
 cin >> again;

 }while((again['Y']) || (again['y']));

 return 0;

}
try putting maxNum on the left, as in

maxNum = userNumOne

I don't know if this'll fix it, but I always do little stuff like that whenever I get compiler errors, and they always seem to fix it. Do the same to minNum (it might not have parsed to it after getting the maxNum error).

And i just noticed the double-equals sign. I'm rather new to C++, too, but I think that this, like in Java, is more of an equality (comparison) sign. Try one equal sign.

Like I said, I'm new, so don't take me to court if it doesn't work.
thanks for the help. it workd =]. but yea now i have another problem.
when the program excecutes. it calculates the first number for every value. so the user inputs nine. the program calculates the highest and lowest as 9 and calculates the first second third number as 9.
i also get an error message when i input y or n to loop/end program.

any suggestions?
I wouldnt assign these new values after you get the input from the user
userNumOne, userNumTwo, userNumThree
meaning
userNumOne = something; would not be good if you want to do other operations on userNumOne

again['Y'] will convert 'Y' to the ASCII
better would be again[0] == 'Y'

typically it is good programming practices to initialize your variable when they are declared when ever possibly because if you don't your max, or min could be some really ugly number.

The problem with assign a value to max or min how will you know what to assign it. You may assign 0 to max and all the number could be negative. I think the best thing to do in that can is assign it to the first value inputted.
look, there are certain issues in your program:

1. None of your variables are initialized. C++ being a native language, doesn't automatically initializes anything. Now when you declare a variable, it simply allocates some storage space for it. That storage space will contain some value and not zero ( as u may be expecting ).

2. userNumOne == maxNum;
This statement doesn't makes any sense, were u trying to use '=' instead. '==' is a relation operator it does not assigns anything.

3. you are using string object called as "again", now writing like again['y'] is not at all similar to what your intention may be. it calls the overloaded operator[] of string class, which is a random access operator. You are asking the string object to return you a char value located at a index of 97 (the ascii value of 'y'), your string doesn't have that many characters, that's what the error is which u r getting.
If you are new to c++, then read about overloading operator[] and string class of stl. what you could have simply done is

1
2
3
4
5
6
7
8
9
10
char again;              //instead string.
.....
.....

cin >> again;
.....
.....
while ( again == 'Y' || again == 'y')
.....
.....


lastly your program has a logical error.
a proper logic would be

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
maxNum = userNumOne

	if (userNumOne < userNumTwo)
	{	maxNum = userNumTwo;
	}

	if ( maxNum < userNumThree)
	{	maxNum = userNumThree ;
                } 
    
minNum = userNumOne;

	if ( userNumOne > userNumTwo)
	{	minNum = userNumTwo ;
                }

	if ( minNum > userNumThree)
	{	 minNum = userNumThree ;	
                }

hope you can compile and run !!
Topic archived. No new replies allowed.