Hey! As a part of a C++ class, I've been trying to create a program that takes three numbers from a user and multiplies each by 100, and gives them the result. Instead of getting the intended result, I'm getting a random string of numbers. If anyone could let me know as to why, I'd really appreciate it. Thank you!
Put line 17 after 22, line 18 after line 29 and line 19 after line 36.
You have to do that because the C++ has a "linear read". So, if you say firstProduct = firstLength * centimeters but you do not give a value to firstLenght, it won't do the operation properly, even assigning a value later.
1. like Omni said your code is a bit out of order.
2. You are also getting random values because you are not initializing the value of your variables.
3. You can write your code with less variables like this.
#include <iostream>
usingnamespace std;
int main() {
int firstLength = 0;
int secondLength = 0;
int thirdLength = 0;
int centimeters = 100;
cout << "The length ";
cin >> firstLength;
cout << "meters is equal to ";
cout << firstLength * centimeters;
cout << " centimeters." << endl;
cout << endl;
cout << "The length ";
cin >> secondLength;
cout << "meters is equal to ";
cout << secondLength * centimeters;
cout << " centimeters." << endl;
cout << endl;
cout << "The length ";
cin >> thirdLength;
cout << "meters is equal to ";
cout << thirdLength * centimeters;
cout << " centimeters." << endl;
cout << endl;
// Source: http://www.ntu.edu.sg/home/ehchua/programming/cpp/cp0_introduction.htmlreturn 0;
}
I would however recommend using a loop so you do not repeat your self so much and you can count oif what entry the user is putting in. Also we can make your output a bit more descriptive this way.