Getting an extra \n somehow.

Hello,

My code is printing out an extra \n somewhere and I cannot figure it out. I run the code against a testbed that replies with user input. The code works fine until the user puts in improper pricing. In order to manage that I put in the do While loop (or the if statements bellow it) but unfortunately this does not stop the problem. I don't think I am able to put a picture here of my test bed results so I'll type it exactly as it's shown.


Enter name: bomb
Enter description: break the code
Enter weight: 14
Enter price: -12
Enter price: I'm not sure
\n <-- here is the problem new line
Enter price: 40

It seems the test bed's answer includes a new line in it like a return, but I don't know why it would do it twice. Or how to handle it.


Running it by itself actually causes the program to terminate at the I'm not sure input.. This is probably where the error is but I don't see anything wrong with my isAlpha.. Any ideas are appreciated.



Here is the problem code. I don't think anything else should be needed out of my program.

Remember this is run against a testbed software that gives it's own input.

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
  int main()
{

   string name;
   string description;
   float weight;
   float basePrice;
   
   cout << "Enter name: ";
   getline(cin , name);
   cout << "Enter description: ";
   getline(cin, description);
   cout << "Enter weight: ";
   cin >> weight;
   
   
   //cannot get stupid extra \n to dissapear
   
   do 
   {
     cout << "Enter price: ";
     cin >> basePrice;
   }
   while (isalpha(basePrice) || basePrice < 0);
   
   
   /*
   cout << "Enter price: ";
   cin >> basePrice;
   
   if (isalpha(basePrice) || basePrice < 0)
   {
      cout << "Enter price: ";
      cin >> basePrice; 
   }   
   
   */
   return 0;
}
Last edited on
In future, post a complete program (including the includes).
isalpha(basePrice) makes no sense. isalpha takes an int (representing a single char), not a float.

Maybe you want something 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
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

double getFloat(string prompt) {
   double val = 0;
   while (true) {
       cout << prompt << ": ";
       cin >> val;
       if (cin) break;
       cout << "This value must be a floating point number.\n";
       cin.clear();
       cin.ignore(99999, '\n');
   }
   return val;
}

int main() {
   double weight = getFloat("Enter weight");
   double price = getFloat("Enter price");
   cout << "weight: " << weight << '\n';
   cout << "price : " << price << '\n';
   return 0;
}

Hello jcthomas556,

I worked with your program, but alas, I could not duplicate your described problem.

tpb makes a good point about the "isalpha". If you have not take a look at http://www.cplusplus.com/reference/cctype/isalpha/

The do/while loop is going everything in the wrong way.

First what you need to understand is cin >> basePrice;. This is known as formatted input. Because "basePrice" is defined as a numeric variable that i what is expected from "cin". When you enter something other than a number it caused "cin" to fail and sets the fail bit of "cin".

Your do/while loop works better like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
cout << "Enter price: ";
cin >> basePrice;

while (!cin || basePrice < 0)
{
	std::cout << "\n Invalid entry try again.";

	if (basePrice < 0)
		std::cout << "\n Price must be greater tha 0." << std::endl;
	else
	{
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	}

	cout << " Enter price: ";
	cin >> basePrice;
}

Notice that after printing an error message the next line, the if statement checks for less than 0 otherwise it clears the state bits and clears the input buffer. For line 13 tpb's method works, but this I believe is the more accepted and most used method. The last two lines allow you to enter a new price and stays in the while loop until a valid price is entered.

You might make the do/while loop work, but this is what I see more often.

Since there are two variables that take a numeric value tps's use of the function is a good idea and I do like the way it works. Something to think about for the future. Also if the function is generic enough you can use it in other programs.

It is better to use "double" over "float" as a double is more accurate and with the greater precision it will store numbers better. These days the "double" is preferred over the "float".

Since "weight" is a numeric variable a similar while loop would be a good idea after you enter the "weight".

Hope that helps,

Andy
Thanks Andy, though not as neat as your code this is what I ended up doing. I was misunderstanding my own code a bit and you pointed that out well.
Hello jcthomas556,

You are welcome. Sorry I got a it behind.

With time you will develop your own style of coding that is appropriate.

There are many styles. The best style is the one that is easiest to read. Read enough posts here and you will get some input and ideas of what works best.

A bigger example of something I did is at http://www.cplusplus.com/forum/beginner/237365/#msg1060573 Do not worry if you do not understand it pay more attention to the overall format.

Hope that helps,

Andy
Topic archived. No new replies allowed.