My first program

This is a discussion on My first program within the C++ Programming forums, part of the General Programming Boards category; Hi all, I am creating a calculator with the ability to calculate speed, distance and time for my first program, ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    128

    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
    478
    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
    ZuK
    ZuK is offline
    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
    Registered User
    Join Date
    Feb 2010
    Posts
    128
    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, 12: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, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21