0

I am trying to create a program that takes in 3 numbers and puts them in ascending order.

I have written out the code out but I keep getting an error saying:

"Error C4700 uninitalized local variable "num2" used.

"Error C4700 uninitialized local variable "num3" used.

I've done If/if-else/else nesting before but I've never ran into this error. I am new to C++ and still learning.

I've tried setting the variables to 0 for default and then asking in for a number so it will be overwritten and then I can compare them to each other. And I also get a random run-time error as well when I do not set them to 0;

Here is the code:

#include <iostream>
using namespace std;

int main()
{

int num1; //I get an error message when I do not set it to 0.
int num2;
int num3; 

cout << "Please enter three numbers: " << endl;
cin >> num1; num2; num3; 

if (num1 > num2 && num2 > num3){
    cout << "In ascending order: " << num1 << num2 << num3 << endl;
}
else if (num2 > num1 && num1 > num3)
{
    cout << "In ascending order: " << num2 << num1 << num3 << endl;
}
else if (num3 > num1 && num1 > num2){
    cout << "In ascending order: " << num3 << num1 << num2 << endl;
}
else
{
    cout << "There was an invalid input." << endl; 
}

system("pause");
return 0;
}

Here are the errors I'm getting.

vonbrand
  • 11,412
  • 8
  • 32
  • 52
Burori
  • 1
  • 3

1 Answers1

2
cin >> num1; num2; num3; 

This probably should be

cin >> num1 >> num2>> num3; 

A few other comments:

1) Avoid "use namespace std;"

2) Try to find a more productive C++ development environment that doesn't force you to waste time working around such silliness as system("pause")

Community
  • 1
  • 1
Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • I'm currently in a c++ class at my community college and he said this is the workaround for the moment before we get deeper into the lessons. Also thank you for your help. – Burori Jan 29 '16 at 00:36
  • And I do not know what to do if I do not use "using namespace std;" This is what our teacher has told us to use everytime we want to create a program. – Burori Jan 29 '16 at 00:38
  • 1
    You are not being taught good C++ programming. The problem is that when someone has good C++ skills, they'll make a lot more money doing something else, other than a teacher. Therefore, most so-called C++ "teachers" don't really know much about C++. If you are looking towards a career as a developer, it's up to you to go out on your own, and read as much as you can, when it comes to C++ development. You can start by actually following the link that I posted, explaining why "use namespace std;" should be avoided, and what should be done instead. Did you read it, and if not, why not? – Sam Varshavchik Jan 29 '16 at 01:02
  • I did not realize those were clickable links. I thought they were just highlighted blue to display code. I understand now why not to use "Using namespace std;", but what do I do for system("pause")? I read to use a breakpoint. Does that mean to put "break;" at the end of the code, or? – Burori Jan 29 '16 at 18:15