And I'm using Microsoft Visual C++ Express....
Now, I've been tasked in the exercise i'm doing to ask for three integers and then read them out in size order.
I'm not supposed to know multi-purpose if/else statements yet (as in if and else's with multiple statements thus requiring { } bracketing, but i'm using them anyway because i thought they'd reduce the size of my code. (if there's a way to solve this without them and without it being larger/messier please tell me.)
So i'm supposed to know if statements, single line option if/else statements and assignments.
This is what i wrote (it compiles ok and works fine in windows, giving good results....i think). The comments were there for me to rationalise it when i got errors (0's popping out).
How bad is it, what could i have done better and what bad habits do i already have? (remember i've not been introduced to much yet in my book and i'm trying not to run before i can walk).
- Code: Select all
int main()
{
cout <<"Please enter three whole number values: \n";
int val1;
int val2;
int val3;
cin >> val1 >> val2 >> val3;
int starter = 0;
int middle = 0;
int final = 0;
int unknown_small = 0;
if (val1 > val2) // if 1 is bigger than 2
{
middle = val1; // middle is assigned value of val1
starter = val2; // and starter is assigned value of val2
}
else
{
middle = val2; // middle is assigned value of val2
starter = val1; // and starter is assigned value of val1
} // Both starter and middle now non-zero value
if (val3 > middle) // compare value assigned to middle to third number
{
final = val3; // if third number is bigger final is assigned value of val3
unknown_small = middle; //need to eliminate zero value
}
else
{
unknown_small = val3; // new integer unknown_small is given value of val3 if val3 not biggest
final = middle; // and since we have the biggest number it's assigned final
}
if (unknown_small > starter) // third value(unknown_small) smaller than middle - smaller than first value?
middle = unknown_small; //if not middle value becomes unknown_small
else
{
middle = starter;
starter = unknown_small;
}
cout <<starter <<" , " <<middle <<" , " <<final <<'\n';
}

