Thread: My first program

  1. #1
    george7378
    Guest

    My first program

    Hi all,

    I am creating a calculator with the ability to calculate speed, distance and time for my first program, and I am almost finished. I just have one problem:

    The program starts with a selection menu, asking the user to enter 1, 2 or 3 to take them to the speed, distance and time calculators. I have the following code:

    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
    	
      // declaring variables:
      float s, d, t, a;
      
    
      // process:
      s = 0;
      d = 0;
      t = 0;
      
      a = 0;
    
    start8:
      cout << "Type 1 (speed), 2 (distance) or 3 (time) and press enter: ";
      cin >> a;
      if (a = 1)
      goto start1;
      if (a = 2)
      goto One2;
      if (a = 3)
      goto Two3;
    With the code for each of the different calculators underneath, in the following setup:

    Code:
    start1:
     cout << "To get speed (m/s), enter distance (m), and press enter: ";
     cin >> d;
     cout << "To get speed (m/s), enter time (s), and press enter: ";
     cin >> t;
     s = d / t;
     cout << "\n";
     cout << "Speed (m/s) is: ";
     cout << s;
     cout << "\n";
     cout << "\n";
     goto start8;
    
     one2:
     cout << "To get distance (m), enter speed (m/s), and press enter: ";
     cin >> s;
     cout << "To get distance (m), enter time (s), and press enter: ";
     cin >> t;
     d = s * t;
     cout << "\n";
     cout << "Distance (m) is: ";
     cout << d;
     cout << "\n";
     cout << "\n";
     goto start8;
    
     two3:
     cout << "To get time (s), enter distance (m), and press enter: ";
     cin >> d;
     cout << "To get time (s), enter speed (m/s), and press enter: ";
     cin >> s;
     t = d / s;
     cout << "\n";
     cout << "Time (s) is: ";
     cout << t;
     cout << "\n";
     cout << "\n";
     goto start8;
    
    return 0;
    }
    The problem is that whether I enter one, two or three at the beginning of the program, I always get directed to the speed calculator. What do I need to do to be able to access the other two by entering 2 and 3 at the beginning?

    Thanks.

  2. #2
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Don't use goto, ever. There is no problem that cannot be solved without it, and it should be used as it creates spaghetti code that is near impossible to follow.

    Take a look at your own code. I started reading it, and I saw: start8, goto start1, goto one2, goto two3.. How am I supposed to know where your code is going? Am I supposed to use the search function of my editor? You didn't even use descriptive names!
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    This is assignment.
    Code:
    if (a = 1)
    Compare instead
    Code:
    if (a == 1)
    Kurt

  4. #4
    george7378
    Guest
    Great, it works. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM