2 Replies - 749 Views - Last Post: 20 November 2010 - 02:08 PM Rate Topic: -----

#1 jfresh992  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 20-November 10

having trouble with this program

Posted 20 November 2010 - 12:13 PM

ok i have to make a program in c++ that presents
the user with a menu that looks like the text below.
Choose 1 of the following choices:
1 – Convert Miles to Kilometers
2 – Calculate the distance between two (x,y) coordinates
3 -- Quit
if they choose 1- i need to ask them if they want to randomly generate numbers to convert to kilos or use there own. same thing with 2. this is wht i have so far,it compiles ok but the stuff doesnt do what i choose except 3.

#include<iostream>
#include<cmath>
#include<cstdlib>
#include<ctime>
using namespace std;

void instruct()                   
 {
    cout << "This program does two things."<endl; 
    cout<<"It computes the distance between two points and converts miles to kilometers using random number generation"<<endl;
    cout<<"Or your own numbers, and it converts miles to kilometers the same way." <<endl;
    cout<<"Enter the what function you want to do and follow the in "<<endl;
    cout<<"program instructions that will help you succesfully complete the function"<<endl;
	
}

void menu()
{ 
cout<<"Choose one of the following choices:"<<endl;
        
cout<<"1-Convert miles to kilometers"<<endl;
        
cout<<"2-Calculate the distance between two coordinates"<<endl;
        
cout<<"3-Quit"<<endl;
}

int main(){
   

srand(time(0) ); 
int min,max,randx1,randy1,randx2,randy2,temp;
randx1=min+max+rand()%(max-min+1);
randy1=min+max+rand()%(max-min+1);
randx2=min+max+rand()%(max-min+1);
randy2=min+max+rand()%(max-min+1);
string yes;
string no;
int choice;
int num;
double KM_PER_MILE = 1.609;    // for storing a constant: 1.609 km equals a mile

double miles;             // for storing the input data: distance in miles


double kms;          // for storing the result: distance in kilometers

double distance;     // distance of two points formula

int x1, x2, y1, y2;          // coordinates 

cout<<"Choose one of the following choices:"<<endl;
        
cout<<"1-Convert miles to kilometers"<<endl;
        
cout<<"2-Calculate the distance between two coordinates"<<endl;
        
cout<<"3-Quit"<<endl;
        
cin>>num;

switch (num)
{
       
case 1:
cout << "Would you like to use random number generation or enter your own numbers?"<<endl;
break;

case 2:
cout << "Would you like to use random number generation or enter your own numbers?"<<endl;
// rest of code here
break;

case 3:
cout << "End of program"<<endl;
// End of program statements
	cout << "Please press enter once or twice to end...";
	cin.ignore().get();    			// hold console window open
	return EXIT_SUCCESS;           	// successful termination
break;

default:
cout << "Not a Valid Choice";
cin>>num;
break;
}

if (num==1)
{
           cout<<"Answer yes for random number generation and no to choose your own numbers"<<endl;
           cin>>choice;

           if (choice=='yes')

           cout<<"Enter minimum value"<<endl;
           cin>>min;
           
           cout<<"Enter maximum value"<<endl;
           cin>>max;

if (min>max)
  min=temp;
  max=min;
  min=max;
}
         
           miles=randx1;    
           
           cout<<"The distance in miles is"<<miles<<endl; 

      kms = KM_PER_MILE * miles;

      cout << "The distance in kilometers is:"<<kms<< endl;
           
             
 if (choice=='no')
{
                 cout<<"Enter miles"<<endl;
                 cin>>miles;
                 
                  kms = KM_PER_MILE * miles;

      cout << "The distance in kilometers is:"<<kms<< endl;
           }
//***************************************************************           
if (num==2)
{
        cout<<"Answer yes for random number generation and no to choose your own numbers"<<endl;
        cin>>choice;
        }
if (choice=='yes')
{                       
           cout<<"Enter minimum value"<<endl;
           cin>>min;
           
           cout<<"Enter maximum value"<<endl;
           cin>>max;
}
if (min>max)
 {
  min=temp;
  max=min;
  min=max;
}       
         distance = sqrt(((randx1+randx2)*(randx1+randx2))+((randy1+randy2)*(randy1+randy2)));  

         cout<<"The distance in miles is"<<distance<<endl;  
 if (choice=='no')
  {  
    cout << "Enter x1: ";        // prompt user to input first point
    cin >> x1;                   // read data from keyboard
    
    cout << "Enter x2: ";        // prompt user to input second point
    cin >> x2;                   // read data from keyboard
    
    cout << "Enter y1: ";        // prompt user to input third
    cin >> y1;                   // read data from keyboard
    
    cout << "Enter y2: ";        // prompt user to input fourth
    cin >> y2;                   // read data from keyboard
    
           
    distance = sqrt(((x1-x2)*( x1-x2 ))+ ((y1-y2)*( y1-y2 )));
    
    cout << "The distance is "<<distance<<endl;
}
   // End of program statements
	cout << "Please press enter once or twice to end...";
	cin.ignore().get();    			// hold console window open
	return EXIT_SUCCESS;           	// successful termination

}


MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 20 November 2010 - 12:15 PM


Is This A Good Question/Topic? 0
  • +

Replies To: having trouble with this program

#2 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: having trouble with this program

Posted 20 November 2010 - 01:59 PM

Instead of using a an int variable type for your num variable, why not use it as a char, and when you go into your switch statements just use single quotes denoting the char variable type?

char num;

cout << "Please choose 1, 2, or 3: ";
cin >> num;

switch(num)
{
   case '1':
      cout << "this should work" << endl;
      break;
   case '2':
      cout << "This should work for number 2" << endl;
      break;
   case '3': 
       cout << "This should work for number 3\n";
       break;
    default:
      "Invalid choice\n"
      break;
}


Was This Post Helpful? 0
  • +
  • -

#3 reaper4334  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 107
  • Joined: 01-November 09

Re: having trouble with this program

Posted 20 November 2010 - 02:08 PM

You seem to be missing a few curly brackets

For example, your if statement on line 93.. as far as I can tell it should start on 94 with a { and close on line 114/115 with a } but it doens't.
Same goes for you if statement on line 101

Aside from that, the only thing I'll say is try to structure your code a little better so it's easier to read.
That would be a great help for us to read it if you want help :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1