...

View Full Version : Need help in do while..



flint0131
08-10-2009, 09:37 PM
don't use any advance C++ libraries coz I'm a newbie.. D:
Anyway, I'm having a problem with my program..
Here's the code :


#include <stdio.h>
#include <conio.h>
#include <iostream.h>
#define PI 3.14

int main(void) {
clrscr();
char userInput;
do {
cout<<"Enter an Option :\n";
cout<<"A - Calculates and displays the Area of a circle.\n";
cout<<"C - Calculates and displays the circumference of a circle.\n";
cout<<"V - Calculates and displays the volume of a cylinder.\n";
cout<<"Q - Quit\n\n";
cin >> userInput;

if ( userInput = 'Q' ) {
int rad_A, sol_A;
cout<<"Enter Radius : ";
cin >> rad_A;
sol_A = PI * (rad_A*rad_A);
cout<<"The Area of a circle is : "<< sol_A;
cout<"\n\n";
} else if ( userInput = 'C' ) {
int rad_C, sol_C;
cout<<"Enter Radius : ";
cin >> rad_C;
sol_C = 2*(PI)*rad_C;
cout<<"The circumference of the Circle is : "<< sol_C;
cout<<"\n\n";
} else { }
} while ( userInput == 'Q' || userInput == 'C' || userInput == 'V' || userInput == 'Q' );
getch();
}

Here's the problem :
This one is not yet finished but I think I'm having problems with my if else..
Whenever I input "C", it executes the statement on the
condition ( userInput = 'Q' ). Why is that? D:

I want to make a program that loops the Option and stops until the user inputs Q. D: I really need help..

oracleguy
08-10-2009, 09:48 PM
Simple.

Because userInput = 'Q' is assigning 'Q' to userInput and it is returning true. Which will cause it to execute that code. You need to use the comparison operator instead: userInput == 'Q'. You'll need to make that fix for your other if statement as well.

flint0131
08-10-2009, 10:09 PM
Thank you so much! I made the program runs.. Thanks for helping a noob like me..

Now, I have another problem..

I want to..
if the userInput is not equal to A,C,V and Q. It will still loop.. D:

oracleguy
08-10-2009, 10:16 PM
Based on your menu, should Q quit the program? So as long as the user input isn't Q, it should keep looping?

flint0131
08-10-2009, 10:25 PM
Yes..

Anyway, I answered my question.. Haha.
Wew.. It took me a lot of Trial and Errors.. :eek:

Thanks for your help!

Here's my final code :

#include <stdio.h>
#include <conio.h>
#include <iostream.h>

int main(void) {
clrscr();
char userInput;
char notValid[4];
notValid[0] = 'A';
notValid[1] = 'C';
notValid[2] = 'V';
notValid[3] = 'Q';
for ( int g = 0; g < 4; g++ ) {
notValid[g];
}
do {
cout<<"Enter an Option :\n";
cout<<"A - Calculates and displays the Area of a circle.\n";
cout<<"C - Calculates and displays the circumference of a circle.\n";
cout<<"V - Calculates and displays the volume of a cylinder.\n";
cout<<"Q - Quit\n\n";
cin >> userInput;

int pi = 3.14;

if ( userInput == 'A' ) {
int rad_A, sol_A;
cout<<"Enter Radius : ";
cin >> rad_A;
sol_A = pi * (rad_A*rad_A);
cout<<"The Area of a circle is : "<< sol_A;
cout<<"\n\n";
} else if ( userInput == 'C' ) {
int rad_C, sol_C;
cout<<"Enter Radius : ";
cin >> rad_C;
sol_C = 2*(pi)*rad_C;
cout<<"The circumference of the Circle is : "<< sol_C;
cout<<"\n\n";
} else if ( userInput == 'V' ) {
int rad_V, height, sol_V;
cout<<"Enter Radius : ";
cin >> rad_V;
cout<<"Enter Height : ";
cin >> height;
sol_V = pi*(rad_V*rad_V)*height;
cout<<"The volume of the Cylinder is : "<< sol_V;
cout<<"\n\n";
} else if ( userInput == 'Q' ) {
cout<<"Bye Bye!";
// nothing really
break;
} else {
cout<<"Fatal Error!\n\n";
}
} while ( userInput == 'A' || userInput == 'C' || userInput == 'V' || userInput == 'Q' || userInput != notValid[g] );
getch();
}

I know it's noobish.. :(

oracleguy
08-10-2009, 10:39 PM
Yeah you seem to be doing a lot of unnecessary things.


for ( int g = 0; g < 4; g++ ) {
notValid[g];
}


What is the point of this. Also I notice you use g again later, you shouldn't because it is out of scope. Let me guess, you are using Visual Studio or Visual C++ Express? You can't count on that working on other compilers.

Here is a more cleaned up version of your program so you can see what you should be doing differently.



#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main(void) {
clrscr();
char userInput;

do {
cout<<"Enter an Option :\n";
cout<<"A - Calculates and displays the Area of a circle.\n";
cout<<"C - Calculates and displays the circumference of a circle.\n";
cout<<"V - Calculates and displays the volume of a cylinder.\n";
cout<<"Q - Quit\n" << endl;
cin >> userInput;

int pi = 3.14;

if ( userInput == 'A' ) {
int rad_A, sol_A;
cout<<"Enter Radius : ";
cin >> rad_A;
sol_A = pi * (rad_A*rad_A);
cout<<"The Area of a circle is : "<< sol_A;
cout<<"\n" << endl;
} else if ( userInput == 'C' ) {
int rad_C, sol_C;
cout<<"Enter Radius : ";
cin >> rad_C;
sol_C = 2*(pi)*rad_C;
cout<<"The circumference of the Circle is : "<< sol_C;
cout<<"\n" << endl;
} else if ( userInput == 'V' ) {
int rad_V, height, sol_V;
cout<<"Enter Radius : ";
cin >> rad_V;
cout<<"Enter Height : ";
cin >> height;
sol_V = pi*(rad_V*rad_V)*height;
cout<<"The volume of the Cylinder is : "<< sol_V;
cout<<"\n" << endl;
} else if ( userInput == 'Q' ) {
cout<<"Bye Bye!" << endl;
// nothing really
break;
} else {
cout<<"Invalid selection!\n\n";
}
} while ( userInput != 'Q' );
getch();
}

flint0131
08-10-2009, 11:11 PM
The point of this code :



for ( int g = 0; g < 4; g++ ) {
notValid[g];
}


is to loop the invalid characters so if the userInput is != to notValid array, it will loop again. LOL. Anyway, I'm just a newbie.. I'll practice a proper way of coding when I master C++.

I don't even know using std. O.o

oracleguy
08-10-2009, 11:42 PM
The point of this code :



for ( int g = 0; g < 4; g++ ) {
notValid[g];
}


is to loop the invalid characters so if the userInput is != to notValid array, it will loop again.

Yeah, that bit of code doesn't actually do anything except waste cpu cycles since there is nothing in the body that does anything.



LOL. Anyway, I'm just a newbie.. I'll practice a proper way of coding when I master C++.

Practicing the proper way is how you get to the master level. :cool:


I don't even know using std. O.o

Yeah you do that when you use the modern header files. You were using "iostream.h" which is an older header file. C++ apps should use "iostream".

The using statement means to bring in a command from a namespace. So using std::cout; means to import cout from the std namespace. You'll learn about namespaces eventually I'm sure, they are a more advanced topic.

BWiz
08-12-2009, 08:44 AM
Just a heads up, if you have a set number of comparisons (e.g. you are checking for A, C, V and Q) for code readability purposes use a switch statement rather than if statements.



switch (cVarToCheck) {
case 'A':
case 'C':
case 'V':
case 'Q':
break;
default:
}
Also, depending on which compiler you are using, a switch statement will make the compiler set up a jump table with the assembly code rather than using cmp and jmp instructions, meaning that your code will (hopefully) run faster. Of course, optimizing compilers do this with your code anyway.

You can read up on this stuff by googling it.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum