Mysteries of the cin buffer (or int)?

I have a problem with my code, its supposed to be the beggining of an Invoice calculator, it runs fine as long as you answer the first question with a whole number, if anything else... weird things happen.. run it and let me know what I should do.
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <windows.h>
using namespace std;

int main () {
  ofstream myfile;

  myfile.open ("Invoice.txt");
    cout << "Enter the amount of hours spent in total that" << endl << "it took to repair"
    " the device." << " (also use this" << endl << "tool to calculate"
    " remote support hours):" << endl << endl;

        int hours;
        cin >> hours;

    myfile << "Cost for Hours spent: $" << 10 * hours;
        system("CLS");



     cout << "Did you perform virus removal?:" << endl << endl;

        string virus;
        cin >> virus;

            if (virus=="yes"){
    myfile << endl << "Price for virus removal: $35";
            }
            else if (virus=="Yes"){
    myfile << endl << "Price for virus removal: $35";
            }
            else if (virus=="YES"){
    myfile << endl << "Price for virus removal: $35";
            }
            else{
    myfile << endl << "Price for virus removal: $0";
            }
        system("CLS");



     cout << "Did you perform physical cleaning of the device?:" << endl << endl;

        string pClean;
        cin >> pClean;


            if (pClean=="yes"){
    myfile << endl << "Price for physical cleaning: $10";
            }
            else if (pClean=="Yes"){
    myfile << endl << "Price for physical cleaning: $10";
            }
            else if (pClean=="YES"){
    myfile << endl << "Price for physical cleaning: $10";
            }
            else{
    myfile << endl << "Price for physical cleaning: $0";
            }
        system("CLS");


  myfile.close();


  return 0;
}
I recommend that you read these, and find the one that is applicable to you. Problems with cin are discussed frequently within these forums. You could also search for a similar thread.

I'm confident that the answer to one of these FAQs will help you. In fact, I can see that you are using cin for both numeric and string input which will not work well, unless you are properly cleaning up the input stream after using it each time. Section 15.3 and 15.6 will be of particular interest to you. When using cin, it is a good idea to follow good habits such as checking for invalid inputs and clearing out any carriage returns after string input. Alternatively, use getline for string input.
http://www.parashift.com/c++-faq-lite/input-output.html
eh it still produces weird results when i try to do it properly
also how can i do an or statement so i dont have to have as many else if statements like this or something
if(pClean=="yes")!("Yes")!("YES")
Last edited on
also how can i do an or statement so i dont have to have as many else if statements like this or something
if(pClean=="yes")!("Yes")!("YES")


if (pClean=="yes" || pClean=="Yes" || pClean=="YES" )

if you wanted to do an and for something

 
if (pClean=="yes" && virus=="yes") 


also if you want you can get rid of the brackets

1
2
3
4
if (pClean=="yes" || pClean=="Yes" || pClean=="YES" )
    myfile << endl << "Price for physical cleaning: $10";
else
    myfile << endl << "Price for physical cleaning: $0";
Last edited on
 
if(toupper(pClean[0]) == 'Y')
Topic archived. No new replies allowed.