ERROR HELP - Simple Algorithm

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!

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

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    int firstLength;
    int secondLength;
    int thirdLength;
    int firstProduct;
    int secondProduct;
    int thirdProduct;
    int centimeters = 100;

    firstProduct    = firstLength*centimeters;
    secondProduct   = secondLength*centimeters;
    thirdProduct    = thirdLength*centimeters;

    cout << "The length ";
    cin >> firstLength;
    cout << "meters is equal to ";
    cout << firstProduct;
    cout << " centimeters." << endl;
    cout << endl;

    cout << "The length ";
    cin >> secondLength;
    cout << "meters is equal to ";
    cout << secondProduct;
    cout << " centimeters." << endl;
    cout << endl;

    cout << "The length ";
    cin >> thirdLength;
    cout << "meters is equal to ";
    cout << thirdProduct;
    cout << " centimeters." << endl;
    cout << endl;

// Source: http://www.ntu.edu.sg/home/ehchua/programming/cpp/cp0_introduction.html


    return 0;
}
closed account (iGLbpfjN)
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.

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

using namespace 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.html


    return 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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;

int main() {

    int numberOfInputs = 3;
    int len = 0;
    int centimeters = 100;
    for (int i = 0; i < numberOfInputs; ++i) {
        int currentEntryCount = i + 1;
        cout << "Enter length " << currentEntryCount << ":";
        cin >>  len;
        cout << "Entry " << currentEntryCount << " of " << len << " meters is equal to ";
        cout << len * centimeters;
        cout << " centimeters." << endl;
        cout << endl;
    }
    return 0;
}


If you have any questions feel free to ask!
Last edited on
Topic archived. No new replies allowed.