0

I'm trying to write a program which takes measurements from a user and inputs them into a vector. The while loop continues until the user inputs '|' at which point it breaks out of the loops and prints the measurements. The problem I am having however is that when trying to add the measurements to the vector. I have used the debugger and found that the loops never actually enters the for loop, thus not being able to reach the "push_back statement".

This program is part of the drills in the Bjarne Stroustup PPP c++ book.

 #include "../../std_lib_facilities.h"

double metreConvert (double userInput , String unit) {

if (unit == "cm")
userInput = userInput / 100;
else if (unit == "in")
userInput = (userInput * 2.54) / 100;
else if (unit == "ft")
userInput = ((userInput * 12) * 2.54) / 100;
else if (unit == "m")
userInput;

return userInput;
}

void numbers() {
double input; 
String unit;
vector <double> measurements;

    while (cin >> input >> unit && input != '|'){
    if (unit == "cm")
    input = input / 100;
    else if (unit == "in")
    input = (input * 2.54) / 100;
    else if (unit == "ft")
    input = ((input * 12) * 2.54) / 100;
    else if (unit == "m")
    input;
        for (int i=0; measurements.size(); i++){
            if (i == 0 || input != measurements[i]){
            cout << "\nPopping onto vector";
            measurements.push_back(input);
            }
            else {
            cout << "\nMeasurment cannot be placed on vector";

        }
        }
    }
    cout << "input ended";
    }

void main() {
cout << "Please enter a number followed by a unit(metres,cm,inches,ft), type '|' when finished inputing:";
numbers();
 }
2
  • 2
    Please indent your code correctly. Also, what does input; just before the for loop do? Commented Jul 4, 2013 at 17:32
  • measurements.size() -- maybe check if that not 0? Commented Jul 4, 2013 at 17:34

3 Answers 3

3

input is a double. | is a char. They are not the same thing. So cin fails and your while loop is not entered. To do what you are attempting, you need to input the data as a string first, check its value for |, and if not a match then convert it to a double for further processing.

Sign up to request clarification or add additional context in comments.

1 Comment

I know that | is a character and I'm inputting into a double, but I have gotten the code to work without the for loop, I'm adding that in so users cannot add in the same thing twice.
0

What Remy said, plus:

The second part of a for loop declaration is a condition, which should evaluate to either true or false. In your case, your condition is measurements.size()

Problem is that you don't have anything in your measurements vector, so measurements.size() will return 0. Which is equivalent to false. I suspect this is not really what you wanted to do, you probably meant something like:

for (int i=0; i < measurements.size(); i++){

Even with that, your logic is wrong. Assuming you're just trying to add each entered value into the measurements vector (if it is not equal to the previous measurement), I don't see why you need a for loop here at all. This will do what you want:

while (cin >> input >> unit && input != '|')
{
    if (unit == "cm")
        input = input / 100;
    else if (unit == "in")
        input = (input * 2.54) / 100;
    else if (unit == "ft")
        input = ((input * 12) * 2.54) / 100;
    else if (unit == "m")
        input;    //No idea what this is supposed to be - more missing code?

    if (!measurements.size() || input != measurements[measurements.size()-1])
    {
        cout << "\nPopping onto vector";
        measurements.push_back(input);
    }
    else 
    {
        cout << "\nMeasurment cannot be placed on vector";
    }
}

1 Comment

Cheers mate, this was a great help
0

This line

for (int i=0; measurements.size(); i++){

causes the loop to run forever if the measurements vector is not empty (and not at all if the vector is empty). Perhaps you meant

i < measurements.size()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.