tags:

views:

534

answers:

8

hi,

i have done up a program which requires the following:

1) prompts user input account no

2) prompts user input account type

2.1) if account type = a, prompts some input and does a certain formula [if]

2.2) if account type = b, prompts some input and does another formula [else if]

2.3) if account type is wrong(not a or b), prompts error and return to 2 [else]

3) prompts user input to exit [do while]

3.1) if input = y, exits

3.2) if input = n, return to 1

at the moment i'm pretty stuck at 2.3. it prompts the error and goes to 3 instead of returning to 2 to prompt user input.

where is the problem? the else?

been trying the whole day just for this qns.

pls advice and thanks!

+2  A: 

Without seeing any actual code it is impossible to say what is wrong with your attempt. However something like this should work

// ...
string atype;
while(atype != "a" && atype != "b") {
  cin >> atype;
  if(atype == "a") {
    // 2.1
  } else if(atype == "b") {
    // 2.2
  } else {
    cout << "wrong input" << endl;
  }
}
// ...
sepp2k
A: 

post your code so we can find your problem. don't just copy what people have provided.

A: 

If you should do a bit of work when each case happens and you'd like to avoid the "goto" (which you probably should), I'd suggest splitting the 1), 2) and 3) part up in three separate functions.

If the stuff you should do is really trivial, I'd suggest a solution similar to the one posted by sepp2k. However, I would adjust the while loop to a do-while loop (you shouldn't test the atype at the beginning of the loop because technically it is not initialised).

RoaldFre
+2  A: 

You have to use a loop to keep asking for an account type if the type is wrong:

do
{
    //1
    do 
    {
     //2
     if (type =="a")
      //2.1
     else if(type=="b")
      //2.2
     else
      //2.3
    } 
    while (type != "a" and type != "b");
    //3
    if(input == "y")
     return;
}
while (input == "n");
julienln
A: 

ive tried both sepp's and julienln's method but the error msg at 2.3 keeps looping without stopping.

how abt using bool to validate them?

RealiX
A: 

sorry, i realised i put the second do at the wrong place. its all fixed now.

4am at my place now. sleep time.

thanks guys!

RealiX
A: 

i just realise i have a problem on the same qns. damn.

as above, 1 being an account no, i have set as an int.

however, after keying 123 for example, it returns additional numbers in "2313".

this is my code for the cout:

cout << "ACCOUNT NO: " << account   << '\t\t' << "AMOUNT DUE: $" << fixed << setprecision (2) << total << endl << endl;

is it due to the setprecision in the same statement? i think there are no other solution other than setprecision since i require 2 decimal place at the amount due.

anyone?

RealiX
You're more likely to get an answer if you write up a new question.
David Coufal
A: 

i've sorted out the answers. stupidly, i should have used " instead of '.

RealiX