VExpress not running last piece of code

I'm pretty green at programming in general but am completely baffled as to why the last bit of code isn't working.

I'm going through Principles and Practice using C++ and am incorporating vectors and etc.. Everything is going fine, the logic seems ok but when I execute the program it doesn't run the last bit of code to display the values in the vectors.

Slightly confused and getting frustrated as I can not understand why!

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
#include "../../std_lib_facilities.h"

int main()
{
	vector<double> i;
	vector<string> j;
	double temp_input = 0;										
	string unit;

	while (cin >> temp_input >> unit) {	
		i.push_back(temp_input);
		while (unit != "cm" && unit != "m" && unit != "in" && unit != "ft") {					// if not required string, retry
			cout << "\nincorrect unit entered, please only enter cm, m, ft, or in.\n";
			cin >> unit;
		}
		if (unit == "e")																		// if user wants to exit, break while loop
			break;
		j.push_back(unit);
	}
	
	for (int k = 0; k < i.size(); ++k)														// Program not running this code???
		cout << i[k] << j[k] << '\n';

	keep_window_open();

	return 0;
}


edit: On a side note, if I run this in DevC++ it runs it perfectly....
Last edited on
What's the input?
The user is inputting a double then a string in the first while loop.

The second while loop just checks to make sure they used one of four strings after.

Then if the user enters 'e', the loop kicks out without registering that string in the vector.

The last for loop just repeats the inputs.

Sorry if I didn't answer your question correctly but I'm assuming you meant where the user inputs data?
1. How does the code ever get to line 16? If unit was "e" then it wouldn't get past the while loop.

2. If you break after pushing back into i and before pushing back into j then there will be 1 one more element in i than j ... and lines 21 and 22 won't like that.

I think you misinterpreted @helios's question. I believe that it is asking for an example of what the user should type in at run-time (in order to test your code).
Oh my apologies!

During run time you enter a number followed by either ft, m, in, cm.

eg: 100m or 200ft.

If you type 200up it will ask you to correct the "up" into either ft, m, in or cm.

It gets to line 16 after a correct number and string is entered. If you enter 'e' it exits and displays the vectors.

The first while loop just gets inputs as long as they are of the correct type (I can expand on the program to make it completely fool proof but I'm just doing a quick exercise in the book). The second while loop checks to make sure the string is of a desired result.

I have the if statement on line 16 to see if the user wants to exit, which is why the string pushback is after, because I don't want to record the 'e' in the vector.

Like I said, the program runs as expected in Dev C++, but in Visual Express 17 it doesn't run line 21 and was curious as to why.

Thanks again!
When the program passes the inner while loop, unit will be one of cm, m, in or ft. Therefore, it can not be equal to "e". So how does it break from the outer while loop?

EDIT: you seem to be getting away with this NOT because the code gets to line 16, but because you enter number and unit without a space. Then e will be interpreted as part of the number in scientific notation and the outer while condition would be false (because it thinks there is no unit). The code never reaches line 16. Try entering
200 e
with a space between 200 and e.


My second point is that - for the code you have posted here at least - if line 16 were ever reached (and it isn't) i and j would have different numbers of elements. Accordingly, line 22 would access j beyond its end.

What you have currently as lines 16 and 17 should come straight after line 10.
Last edited on
Line 16 gets reached because the inner while loop only runs if you do not input one of four options. If you don't, it repeats itself until you do. If you do enter one of four options, it skips the inner while loop completely.

The if statement is located in the outer while loop.

Checking for an 'e' before the while loop after line 10 is irrelevant because whether or not the inner loop runs, the if statement will go when I want it to check for an exit.

If you type in 100 e straight away, it asks you to correct it to ft, m, cm, or in. If I put the if statement prior to the inner while loop, if I "mistakenly" type 100 e it will exit instead of asking the user to correct themselves.

I understand that if you type in 100e it takes it as scientific notation. I'm not worried about fool proofing it like I said. I'm more interested into how or why Visual Express closes the program before running anything outside of the last loop (conditions are filled by typing 'e' after your last input) but Dev C++ runs the program fully.

Thanks in advance! (Please if what I'm saying isn't making sense, run the programing inputting numbers followed by a space then by either ft, m, in, or cm... Or don't! The program will make you :P. Then after your last input type 'e' then enter. If you build it in Visual Express 17 it shuts down after the last loop. If you build it in DevC++, it runs the program through to the end. Interesting no?)

I'm sure you'll catch this but you'll have to take out my #include and include iostream and vector and using namespace std;
No, it will never reach the break statement (line 17, following the if test on line 16 to be more accurate). Prove this by putting an appropriate cout statement in braces with the break. Because unit can never be "e" after you have forced it to be one of those four options, the test leading to break is completely redundant.

If you use "e" as a sentinel, then if there is no space between the number and it then scientific notation will make it part of the number and not the unit. Try a different letter as sentinel if you don't believe me.


Try this one. Note that it uses x as a sentinel, not e.
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
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
   vector<double> i;
   vector<string> j;
   double temp_input = 0;                                                                          
   string unit;

   cout << "Enter number and unit (with or without spaces)\n";
   cout << "Use unit as x to end\n";

   while ( cin >> temp_input >> unit )
   {     
      if ( unit == "x" ) break;        // <=== if you want to test on a sentinel it has to go here

      i.push_back(temp_input);
      while (unit != "cm" && unit != "m" && unit != "in" && unit != "ft")       // if not required string, retry
      {
         cout << "\nIncorrect unit entered, please only enter cm, m, ft, or in.\n";
         cin >> unit;
      }
      j.push_back(unit);
   }

   for ( int k = 0; k < i.size(); ++k ) cout << i[k] << j[k] << '\n';
}
Last edited on
Topic archived. No new replies allowed.