A basic question about loops

Cobra_skd
10-01-2002, 08:21 AM
Hello again forum

This time another basic puzzler:

Why does my loop not exit when the value of Calcu come below 5?


// Add Sub Div Program
// The program uses the Switch method
// Cobra SKD (01/10/80)


#include <iostream.h>

int main()

{
// Declare Variables

int No1, No2, sum;

// The answer to which calculation will be performed

char Calcu; // Switch seems to work only on Characters

// Being program poper

// Ask for 2 numbers

cout << "Enter First Number: ";

cin >> No1;

cout << "\nEnter Second Number: ";

cin >> No2;


// Ask what the calculation will be

cout << "\nWill you Add(1), Sub(2), Divide(3) or Multiply (4): ";

cin >> Calcu;

// Check to make sure what is entered is a value between 1 and 4 if not
// Then ask again for a number between the required value 1 and 4

while (Calcu >= 5)

{

cout << "\nWill you Add(1), Sub(2), Divide(3) or Multiply (4): ";

cin >> Calcu;

}

// Use the switch method to find the correct code for calculation required

switch (Calcu)

{

// Calcu = 1 (Add) then do below

case '1':

sum = No1 + No2;

cout << "\nYou're calculation is " << No1 << "+" << No2 << "=" << sum << endl;

// To stop the code from contuning once it had done case 1 you must
// break from the switch

break;

case '2':


sum = No1 - No2;

cout << "\nYou're calculation is " << No1 << "-" << No2 << "=" << sum << endl;

// To stop the code from contuning once it had done case 1 you must
// break from the switch

break;


case '3':


sum = No1 / No2;
cout << "\nYou're calculation is " << No1 << "/" << No2 << "=" << sum << endl;

// To stop the code from contuning once it had done case 1 you must
// break from the switch

break;

case '4':

sum = No1 * No2;

cout << "\nYou're calculation is " << No1 << "*" << No2 << "=" << sum << endl;

// To stop the code from contuning once it had done case 1 you must
// break from the switch

}

return 0;

}

Iceplug
10-01-2002, 08:57 AM
Don't you need a semicolon after the while block?

kinki
10-01-2002, 08:57 AM
you should declare Calcu as integer, then remove those " ' " in the case like
change case '1' to case 1.

Cobra_skd
10-01-2002, 09:08 AM
thanks guys always!

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum