Problem when asking for user confirmation!!

Hellooo I am a new beginner of C++.
I've found difficulties when doing the following simple programme.....
Please help!!


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
#include <cstdlib>
#include <string>
#include <iostream>
#include <cctype>


using namespace std;

int main(int argc, char *argv[])
{
    string itemcode, itemdescription;
    double itemweight, itemunitprice;
    int quantityrequired;    
    
    char answer;

    do {
          cout << "Enter item code: ";
          getline (cin,itemcode);
    
          cout << "Enter item description: ";
          getline (cin,itemdescription);  
        
          cout << "Enter item weight in grams: ";
          cin >> itemweight;
    
          cout << "Enter item unit price $: ";
          cin >> itemunitprice;
    
          cout << "Enter quantity required: ";
          cin >> quantityrequired;
    
          double totalweight = itemweight * quantityrequired;
          double totalprice = itemunitprice * quantityrequired;

          cout << "Summary Data for Item "<< itemcode << ","<< itemdescription << endl;
          cout << "Total Weight: " << totalweight << " grams" << endl;
          cout << "Total Price: $" << totalprice << endl;
         
          cout << "Another?(Y/N): ";
          cin >> answer;
          
          if ((answer!='Y')&&(answer!='y'))          
          {
              break;
          }
        } while(1);
        
    system("PAUSE");
    return EXIT_SUCCESS;
}


Problem 1>> When I ask for user confirmation (Y/N), how can I make "Y"="y" (i.e. ignore the lettercase)?? Since before I define answer='Y', if I enter 'y', it sees 'y', 'n' and 'N' as not equal "Y"....then the programme quits.. which is not what I want! I temporarily use (answer!='Y')&&(answer!='y') to present...but it looks stupid...


Problem 2>> If I enter 'Y', it loops again. However, on the screen, the prompt "Enter item code" and "Enter item description" combine into one line. That means when it starts looping, I am not allowed to enter item code and item description anymore...I can only enter the remainings....

Please HELP!!Thank you so much!!!!
It really kills time...I've been spending 3 days on tackling it...(tiring)!!!
Last edited on
For problem 1, see http://cplusplus.com/reference/clibrary/cctype/toupper/

For problem 2, the extraction operator>> extracts data from the input stream UP TO a delimeter character, such as a space, tab, or carriage return. It does not remove the delimiter from the stream though. However, it does ignore leading delimiters, and will toss them out until it reads some valid input data (or data that generates an error, but that's a separate issue).

getline(), on the other hand, does not ignore leading delimiters. If the first character it reads is a delimiter, it just falls through without reading anything else.
look at http://cplusplus.com/reference/iostream/istream/ignore/ to clear the input stream prior to calling getline. This way it will wait for user input and not automatically fall through.
You really should write a yes/no() function. However I think the problem iss at line 44 and is due to the if() statement including && rather than ||.
Last edited on
I got it !!! FINALLY~!!
Thank you guys ~ jRaskell and buffbill.
Topic archived. No new replies allowed.