GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-Oct-2005, 11:50
bco1109 bco1109 is offline
New Member
 
Join Date: Oct 2005
Posts: 12
bco1109 is on a distinguished road

if loop problems


I am having problems with the following progamany ideas?

CPP / C++ / C Code:
void main()

{
	 int choice; 
	 int a,b,c,d=0;
	 int is_prime(int n);
     float ctemp,ftemp ;
  	 char Type;	                        
	 cout << "Please enter 'c' to convert temprature between celcius or farenhheit\n";	                
	 cout << "Please enter 'p'' for finding a prime number\n";	                 
	 cout << "Please enter 'q'' to quit\n";	                 
	 cin>> Type; 
	 while (Type !='q' ||'Q'){
		
			if(Type =='c'|| 'C')  
			{
				cout << "1.Celsius to Fahrenheit" << endl;
				cout << "2.Fahrenheit to Celsius" << endl;
				cout << "Choose either 1 or 2 : " << endl;
				cin>>choice;
				if (choice==1)
				{
					cout << "Enter the temperature in Celsius : " << endl;
					cin>>ctemp;
					ftemp=(1.8*ctemp)+32;
					cout << "Temperature in Fahrenheit = " << ftemp << endl;
				}
				else
				{
					cout << "Enter the temperature in Fahrenheit : " << endl;
					cin>>ftemp;
					ctemp=(ftemp-32)/1.8;
					cout << "Temperature in Celsius = " << ctemp << endl;
				}
			}
			if(Type == 'p' || 'P')                                                      			
			{
				printf ("\n Enter a Number : ");
				scanf ("%d",&a);
				for (b=2;b<a;b++)
				{
					c=a%b;
					if (c==0)
					{
						d=1;
						break;
					}
				}
			     switch (d)
				{
				case 1:
				printf ("\n The Numer is NOT a Prime Number");
				break;
				default:
				printf ("\n The Number is a Prime Number ");
				}
			}
                                        
			else 	
				cout << "invalid entry" << endl;                       
	        }                                                       
                                                               
}
Last edited by admin : 11-Oct-2005 at 21:17. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 11-Oct-2005, 12:01
bco1109 bco1109 is offline
New Member
 
Join Date: Oct 2005
Posts: 12
bco1109 is on a distinguished road

Re: if loop problems


The compiler is c++ and here are the program requirements.

rite a C++ program that shows the following menu:

P[rime] C[onversion] Q[quit]

• If you select "P or p" You should invoke a user-defined function, int prime (void) to prompt the user to enter a number and determine whether or not the number is a prime number. Print the number in function main (). (Hint: you may use modulus, % operator)

• If the user select "C or c" You should invoke a user-defined function void convert (void) to convert from Fahrenheit to Celsius or from Celsius to Fahrenheit. In this function prompt the user for the following submenu:

1. Convert from Fahrenheit to Celsius
2. Convert from Celsius to Fahrenheit

The user should choose one of the options. Then prompt the user for appropriate temperature and then display corresponding temperature. Use the conversion equations below:

F = (9/5)C + 32
C = (5/9) (F-32)

• If the user selects “q or Q” then your program should terminate.
  #3  
Old 11-Oct-2005, 12:04
Kacyndra's Avatar
Kacyndra Kacyndra is offline
Member
 
Join Date: May 2005
Location: Maryland
Posts: 226
Kacyndra will become famous soon enough

Re: if loop problems


try this
CPP / C++ / C Code:
if((Type =='c')|| (Type == 'C'))
__________________
Xrum!
  #4  
Old 11-Oct-2005, 12:30
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: if loop problems


Hi bco

Welcome to the GID Forums.

If is not a Loop. for, while, do while are loops. If is just a statement to check whether the condition is true or not.

The main error in your program is that you should put the cin>>Type inside your loop. you should get the input from the user everytime isnt it?

You have to use functions as you said.

The function declaration should be of this type:

CPP / C++ / C Code:
CPP / C++ / C Code:
return-type function-name(parameter list)
{
declarations and statements;
return expression;
}
The return-type specifies the type of data that the function returns. The parameter list is a comma-separated list of variable names and their associated types that receive the values of the arguments when the function is called . All function parameters must be declared individually, each including both the type and name. That is, the parameter declaration list for a function takes this general form:
f(type varname1, type varname2, . . . , type varnameN)

For example consider your prime number function.
it should be like this:
CPP / C++ / C Code:
int  prime(void)
{
//code to find whether a number is a prime number or not

return x; //where x is return value
}

You can construct a similar function for Celsius/fanrenheit conversion.

then in your main program use a do while loop so that you get input from the user atleast once.
You may use this method:

CPP / C++ / C Code:
do
{
//get input;
//use if or switch statements
//use the functions accordingly
} while( Type != 'q'  || Type != 'Q' );

and your
CPP / C++ / C Code:
switch (d)
{
case 1:
printf ("\n The Numer is NOT a Prime Number");
break;
default:
printf ("\n The Number is a Prime Number ");
}
can simplified by using if and else statement because d can be only 1 or 0.

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #5  
Old 11-Oct-2005, 14:15
bco1109 bco1109 is offline
New Member
 
Join Date: Oct 2005
Posts: 12
bco1109 is on a distinguished road

Re: if loop problems


This is what I have so far. I am sure I am missing something however making progress.

CPP / C++ / C Code:
int main(void) 
{
	 int choice; 
	 int a,b,c,d=0;
	 int is_prime(int n);
	 tempconv;
	 int prime;
     float ctemp,ftemp ;
  	 char Type;	                        
	 
	do
	 {
		cout << "Please enter 'c' to convert temprature between celcius or farenhheit\n";	                
		cout << "Please enter 'p'' for finding a prime number\n";	                 
		cout << "Please enter 'q'' to quit\n";	                 
		 cin>> Type; 
		
			if((Type =='c')||(Type =='C')) 
		int tempconv(void)
			{
				cout << "1.Celsius to Fahrenheit" << endl;
				cout << "2.Fahrenheit to Celsius" << endl;
				cout << "Choose either 1 or 2 : " << endl;
				cin>>choice;
				if (choice==1)
				{
					cout << "Enter the temperature in Celsius : " << endl;
					cin>>ctemp;
					ftemp=(1.8*ctemp)+32;
					cout << "Temperature in Fahrenheit = " << ftemp << endl;
				}
				else
				{
					cout << "Enter the temperature in Fahrenheit : " << endl;
					cin>>ftemp;
					ctemp=(ftemp-32)/1.8;
					cout << "Temperature in Celsius = " << ctemp << endl;
				}
				returntemp;
			}
			if((Type == 'p')||(Type== 'P'))   
      int prime (void)
			{
				printf ("\n Enter a Number : ");
				scanf ("%d",&a);
				for (b=2;b<a;b++)
				{
					c=a%b;
					if (c==0)
					{
						d=1;
						break;
					}
				}
			     switch (d)
				{
				case 1:
				printf ("\n The Numer is NOT a Prime Number");
				break;
				default:
				printf ("\n The Number is a Prime Number ");
				}
		return printf
			}
                                        
			else 	
				cout << "invalid entry" << endl;                       
	        } 
while ((Type !='q')||(Type!='Q')) 
{
	 return (main);                                                      
}
Last edited by admin : 11-Oct-2005 at 21:19. Reason: Please insert your C code between [c] & [/c] tags
  #6  
Old 11-Oct-2005, 15:17
bco1109 bco1109 is offline
New Member
 
Join Date: Oct 2005
Posts: 12
bco1109 is on a distinguished road

Still having trouble


I have the program working however I have a few problems.

1. When i quit the progam it continues to loop invalid entry.

2. when I check for a prime number, once I enter a non prime number it will not detect the prime number anymore.

Below is the C++ code I have so far.

CPP / C++ / C Code:
#include <iostream.h>
#include <math.h>
#include<stdio.h>
#include<conio.h>
#include<assert.h>



//function main begins program execution
int main(void) 
{
	 int choice; 
	 int a,b,c,d=0;
	 int is_prime(int n);
     float ctemp,ftemp ;
  	 char Type;	                        
	 cout << "Please enter 'c' to convert temprature between celcius or farenhheit\n";	                
	 cout << "Please enter 'p'' for finding a prime number\n";	                 
	 cout << "Please enter 'q'' to quit\n";	                 
	 cin>> Type; 
	 while ((Type !='q')||(Type!='Q')){
		
			if((Type =='c')||(Type =='C'))  
			{
				cout << "1.Celsius to Fahrenheit" << endl;
				cout << "2.Fahrenheit to Celsius" << endl;
				cout << "Choose either 1 or 2 : " << endl;
				cin>>choice;
				if (choice==1)
				{
					cout << "Enter the temperature in Celsius : " << endl;
					cin>>ctemp;
					ftemp=(1.8*ctemp)+32;
					cout << "Temperature in Fahrenheit = " << ftemp << endl;
				}
				else
				{
					cout << "Enter the temperature in Fahrenheit : " << endl;
					cin>>ftemp;
					ctemp=(ftemp-32)/1.8;
					cout << "Temperature in Celsius = " << ctemp << endl;
				}
			}
			if((Type == 'p')||(Type== 'P'))                                                      			
			{
				printf ("\n Enter a Number : ");
				scanf ("%d",&a);
				for (b=2;b<a;b++)
				{
					c=a%b;
					if (c==0)
					{
						d=1;
						break;
					}
				}
			     switch (d)
				{
				case 1:
				printf ("\n The Numer is NOT a Prime Number");
				break;
				default:
				printf ("\n The Number is a Prime Number ");
				}
			}
                                        
			else 	
				cout << "invalid entry" << endl;                       
	        }  
	 return 0;                                                      
}
Last edited by admin : 11-Oct-2005 at 21:26. Reason: Please insert your C code between [c] & [/c] tags
  #7  
Old 11-Oct-2005, 16:35
Sokar Sokar is offline
Member
 
Join Date: May 2005
Posts: 243
Sokar has a spectacular aura aboutSokar has a spectacular aura about

Re: Still having trouble


Quote:
Originally Posted by bco1109
I have the program working however I have a few problems.

1. When i quit the progam it continues to loop invalid entry.
This,
CPP / C++ / C Code:
while((Type !='q')||(Type!='Q'))
when you use "or"(||) only one has to be true for the whole statement to be true.
But,
CPP / C++ / C Code:
while((Type !='q')&&(Type!='Q'))
"and"(&&) they both must be true for the statement to be true. Since "Type" cannot be both 'q' and 'Q' at the same time when it is lower or upper case it will fail.
Quote:
Originally Posted by bco1109
2. when I check for a prime number, once I enter a non prime number it will not detect the prime number anymore.
I see this,
CPP / C++ / C Code:
if(c==0)
{
	d=1;
	break;
}
But I do not see where you ever change the value to anything else. It is stuck with the value 1.
  #8  
Old 11-Oct-2005, 16:51
bco1109 bco1109 is offline
New Member
 
Join Date: Oct 2005
Posts: 12
bco1109 is on a distinguished road

Re: Still having trouble


Should i use d=1++
  #9  
Old 11-Oct-2005, 17:10
Sokar Sokar is offline
Member
 
Join Date: May 2005
Posts: 243
Sokar has a spectacular aura aboutSokar has a spectacular aura about

Re: Still having trouble


Quote:
Originally Posted by bco1109
Should i use d=1++
I am sure that will not compile. You can only change lvalues and "1" in not a lvalue.
Some information on "lvalue"
http://cplus.about.com/od/cprogrammi...def_lvalue.htm

You might just try reseting "d" to the original value if you do not need it to keep the value "1".
CPP / C++ / C Code:
case 1:
	printf ("\n The Numer is NOT a Prime Number");
	d=0;
	break;
  #10  
Old 11-Oct-2005, 18:38
bco1109 bco1109 is offline
New Member
 
Join Date: Oct 2005
Posts: 12
bco1109 is on a distinguished road

Re: Still having trouble


Thanks for the help. everything is looking good exept fot the termination of the program. If i add a break then it will not loop back.

CPP / C++ / C Code:
#include <iostream.h>
#include <math.h>
#include<stdio.h>
#include<conio.h>
#include<assert.h>



//function main begins program execution
int main(void) 
{																					//
	 int choice;																	//
	 int a,b,c,d=0;																	//
	 int is_prime(int n);															//
     float ctemp,ftemp ;															//	
  	 char Type;																		//
	 cout << "Please enter 'c' to convert temprature between celcius or farenhheit\n";	//                
	 cout << "Please enter 'p'' for finding a prime number\n";						//	
	 cout << "Please enter 'q'' to quit\n";											//
	 cin>> Type;																	//
	 do																				//
	 {																				//
																					//
			if((Type =='c')||(Type =='C'))											//
			{																		//
				cout << "1.Celsius to Fahrenheit" << endl;							//	
				cout << "2.Fahrenheit to Celsius" << endl;							//
				cout << "Choose either 1 or 2 : " << endl;							//
				cin>>choice;														//
				if (choice==1)														//
				{																	//
					cout << "Enter the temperature in Celsius : " << endl;			//	
					cin>>ctemp;														//
					ftemp=(1.8*ctemp)+32;											//
					cout << "Temperature in Fahrenheit = " << ftemp << endl;		//
				}																	//
				else if (choice ==2)												//
				{																	//
					cout << "Enter the temperature in Fahrenheit : " << endl;		//
					cin>>ftemp;														//
					ctemp=(ftemp-32)/1.8;											//
					cout << "Temperature in Celsius = " << ctemp << endl;			//
				}																	//
				else																//
				{																	//
					cout <<"Invalid entry";											//
				}																	//
			}																		//
			if((Type == 'p')||(Type== 'P'))                                         //           			
			{																		//
				printf ("\n Enter a Number : ");									//
				scanf ("%d",&a);													//
				for (b=2;b<a;b++)													//
				{																	//
					c=a%b;															//
					if (c==0)														//
					{																//
						d=1;														//
						break;														//
					}																//		
				}																	//end of innner loop
			     switch (d)															//begining of switch
				{																	//begining of switch
				case 1:																//case if not prime
				printf ("\n The Numer is NOT a Prime Number");                      //print the number is not prime
				d=0;
				break;
				default:															//case if prime number
				printf ("\n The Number is a Prime Number ");							//print the number is prime
				break;																//end of switch
				}																	//end of switch
			}																		//end of inner loop
			else 																	//break out of the loop
				cout << "invalid entry" << endl;								  //print invalid entry
			break;                                  

	  	  
	 }
																	//end of main loop
	 while ((Type !='q')&&(Type!='Q'));											    //While function to quit program
	 cout << "To terminate program" << endl; 


	 return 0;																	//indicate the program ended sucessfully
}																				    //end main function
Last edited by admin : 11-Oct-2005 at 21:25. Reason: Please insert your C code between [c] & [/c] tags
 
 

Recent GIDBlogHalfway done! by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Array 1 dimensional help please asap lion123 C Programming Language 10 18-Feb-2005 21:53
messy loop help please sammacs C Programming Language 6 26-Nov-2004 15:18
Nested for loop with function Tori C++ Forum 11 08-Nov-2004 13:02
Loop problems ambeco C++ Forum 8 03-Mar-2004 10:02

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:07.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.