I have some questions ,i can't solve it .The first one is,


Write a C++ program using the switch statement that does exactly what the following program does:

#include <iostream.h>
#include <math.h>
void main ()
{
   float number;
   int choice;
   cout<< "Enter you number";
   cin>> number;
   cout << "Enter your choice: 1=sin, 2=cos, 3=tan,       4=log ";
   cin >> choice;
   // nested if's:
   if (choice < 1  ||  choice > 4) {
      cout << "wrong choice";
   }
else{                           
      if (choice <= 2){                        
         if (choice == 1){
            cout << sin(number);
         }
         else{                     
            cout << cos(number);
         }
      }
else{                        
         if (choice == 3){
            cout << tan(number);
         }
         else{
            cout << log(number);
         }
      }
   }
}

Seconed question is:
-Write an interactive program that computes the area of a rectangle (area= base * height) or a triangle (area=0.5 * base * height) after prompting the user to type the first character of the figure name (R or T). Your program must display an error message to indicate that the user entered a character other than R or T.

My program look like this:

#include <iostream>
using namespace std;
int main()  {
  float base,heigh;
  char R,T;
  cin>>R ,T;
  cin>>base , heigh;
  if  (char== 'R')
     cout<<base * heigh<<"R" ;
  else  if (char=='T')
      cout<<0.5*base*heigh<<"T" ;
  else 
      cout<<"you enter neither R nor T";

 return 0;
}

After i compiled it i have these errors:

four.cpp:8: error: expected primary-expression before "char"
four.cpp:8: error: expected `)' before "char"

Recommended Answers

All 6 Replies

Seconed question is:
-Write an interactive program that computes the area of a rectangle (area= base * height) or a triangle (area=0.5 * base * height) after prompting the user to type the first character of the figure name (R or T). Your program must display an error message to indicate that the user entered a character other than R or T.

My program look like this:

#include <iostream>
using namespace std;
int main()  {
  float base,heigh;
  char R,T;
  cin>>R ,T;
  cin>>base , heigh;
  if  (char== 'R') //<-Illegal Syntax (1)
     cout<<base * heigh<<"R" ;
  else  if (char=='T')//<-Illegal Syntax (2)
      cout<<0.5*base*heigh<<"T" ;
  else 
      cout<<"you enter neither R nor T";

 return 0;
}

After i compiled it i have these errors:

four.cpp:8: error: expected primary-expression before "char"
four.cpp:8: error: expected `)' before "char"

The error is there because your if's are trying to test R and T (two character variables) against nothing. When you created R and T - you only created two containers for a character, they are not literally "the letter R" and "the letter T".

To test whether the user inputs the correct character (out of a choice of two), you can get by with a simple test:

char keyVal;

if (keyVal == 'A' ||keyVal =='a') //Makes a case-insensitive match.
{
     //do task A;
}else if (keyVal == 'B' || keyVal == 'b')
{
    //do task B;
}else                                     //This is what gets done if your user chooses an illegal value
{
    //do neither A nor B;
}

You can see that you only need one keyVal character because the user can only enter 1 character at a time. From there, you just have to tell the program which character to look for and what to do when it's found.

For your first problem, it looks like you're just trying to make menu selection. You've got a choice of 4 valid inputs, numeric inputs. So here's a rundown of how to use switch-case that should get you on the right track.

I'll skip you're prompt code because its alright - except you need to add a newline character to choice prompt or else your user input will over write your prompt. You know your user is going to choose a integer so you don't need to do anything fancy to get switch-case to work.

int choice = 0;

switch(choice)
{
	case 1:
		//Do option 1;
		/*break; or 'fall-through'*/
	case 2:
		//Do option 2;
		/*break; or 'fall-through'*/
	case 3:
		//Do option 3;
		/*break; or 'fall-through*/
	default:
		//Do whatever you want by default;
}

Put the switch-case where you want the decision to be made in your program. When the program gets to it, it will evaluate whatever is inside the switch. switch(/*I'm what gets evaluated*/) . It will then find the matching case. But you have to be careful that the you've labeled the cases correctly. The syntax for cases is shown above. The odd-ball on the bottom is the default case. It gets evaluated when no other valid input is selected. The default takes care of if the number is out of range.
One last warning, each case should end with a break; otherwise the program will "fall through" to the next case below it. If you want the program to fall through you should comment the line - this just makes it clear to anyone reading your code that you expect it to happen (otherwise it may look unintentional).
Hope this helps out a bit.

Thanks alot BlackJavaBean .
I understood my errer in question two now.
About question one our doctor did not want to see if statement in the program,she wants us to use switch without if statement.

I bet if ur if statements does not give you syntax errors coz U did not declare cos, sin, tan and log and I dont think C++ syntax recognise those variables neither as reserved words....

About question one our doctor did not want to see if statement in the program,she wants us to use switch without if statement.

here is the version of the switch statements, I did not compile it coz my compiler is giving me hard times so U will fix if there is any errors but i have a feeling that we are talking about endless loop somewhere hopefully U will manage to fix it if there is any

#include "stdafx.h"
#include <iostream>

//#include <math.h>
 using namespace std; 

void main()
{
	float number;
	int choice;
	cout<< "Enter the number:   ";
	cin >> number;
	cout << "Enter your choice:  1 = sin, 2 = cos, 3 = tan, 4 = log ";
	cin >> choice;

 do
 {
	 switch(choice)
	 {
	 case 1:
		 cout << "sin  " << number;
			 break;
	 case 2:
		 cout << "cos  " << number;
			 break;
	 case 3:
		 cout << "tan  " << number;
			 break;
	 case 4:
		 cout << "log  " << number;
			 break;

	 default: cout << "Wrong choice";
	 }
 }while (choice <= 4);
}

First:

I bet if ur if statements does not give you syntax errors coz U did not declare cos, sin, tan and log and I dont think C++ syntax recognise those variables neither as reserved words....

sin() , cos() , log() and tan() are valid functions because they were included with the math header file: #include<math.h> Second:

About question one our doctor did not want to see if statement in the program, she wants us to use switch without if statement.

Traicey's formatting should be a excellent guide for how to accomplish this. A very general rule for switch-case is that it can replace almost any if-else ladder. So you just need to replace your if-else's so that the outputs fall into the correct case. Here's another quick example:

int numCows = 0;

//some piece of code that prompts the user to change numCows;

if(numCows==1)
{
	cout << "There is " << numCows << " cow!\n";
}else if(numCows ==2)
{
	cout << "There are " << numCows << " cows!\n";
}else if(numCows ==3)
{
	cout << "There are " << numCows << " cows!\n";
}else
{
	cout << "I don't know how many cows there are!\n";
}
/*The same segement can be re-written without the if-else's using a switch-case*/
int numCows = 0;

//some piece of code that prompts the user to change numCows;

switch(numCows)//Tells switch which variable you want to test
{//<-Don't forget to enclose all of the cases
case 1://<- Literally says, if(numCows ==1)
	cout << "There is " << numCows << " cow!\n";
	break; //Don't fall through

case 2://Literally says, else if(numCows ==2)
	cout << "There is " << numCows << " cows!\n";
	break;

case 3:
	cout << "There is " << numCows << " cows!\n";
	break;

default://In this scenario, this is like the last else - it gets done, if numCows != 1, 2, or 3
	cout << "I don't know how many cows there are!\n";
	/*fall out of the switch-case*/
}
//some code that does the rest

Sorry the code is nonsense, but it just illustrates how if-else's map onto switch-cases. Hope this helps!

Thanks for your help . Now it's work.
Thanks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.