Help! Beginner Programmer....

This is a discussion on Help! Beginner Programmer.... within the C++ Programming forums, part of the General Programming Boards category; Hey, this is my first post, and I am a beginner programmer, just started learning C++ a week ago. This ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    2

    Arrow Help! Beginner Programmer... First Program, has errors....

    Hey, this is my first post, and I am a beginner programmer, just started learning C++ a week ago. This is my first program, but i can not tell what is wrong...

    Objective:
    make a menu
    user types number
    runs program
    ***returns to menu (how do i do this?)

    My Error/s:
    I had them all (IF... IF.... IF....) and whatever value I put in the menu, it would only display the first one (addition). I changed it to (IF... ELSE IF.... ELSE IF...) and now i cant compile, because i have errors... (expected primary expression before "else" and expected ";" before "else") for each place where i have ELSE IF...

    this is my code...

    Code:
    //Mathematics
    
    //Library
    #include <iostream>
    #include <iomanip>
    #include <conio.h>
    #include <cstdio>
    #include <cmath>
    
    //Const
    
    //Var
    using namespace std;
    double ratep, rate, principle, years, x, x1, x2, x3, y, y2, y3, y4, z, z1, z2, z3;
    int X;
    
    //Main
    int main ()
    
    {
     cout << "######## Mathematics ########" << endl;
     cout << "(C) 2010-2011" << endl;
     cout << " " << endl;
     cout << "Type The Number and Press Enter..." << endl;
     cout << " " << endl;
     cout << "(1): Addition" << endl;
     cout << "(2): Subtraction" << endl;
     cout << "(3): Multiplication" << endl;
     cout << "(4): Division" << endl;
     cout << "(5): Simple Interest Calculator" << endl;
     cout << "(6): Credits" << endl;
     cout << "(7): Quit" << endl;
     cin >> X;
    
    if (X==1);
      {
      //Addition
      
      cout << " " << endl;
      cout << "Addition" << endl;
      cout << "X+Y=Z" << endl;
      cout << "What is X?" << endl;
      cin >> x;
      cout << "What is Y?" << endl;
      cin >> y;
      double z (x+y);
      cout << "Z equals to " << z << endl;
      cin.get();
      cin.get();
      return(0);  
      };
    else if (X==2);
      {
      //Subtraction
      
      cout << " " << endl;
      cout << "Subtraction" << endl;
      cout << "X-Y=Z" << endl;
      cout << "What is X?" << endl;
      cin >> x1;
      cout << "What is Y?" << endl;
      cin >> y2;
      double z1 (x1-y2);
      cout << "Z equals to " << z1 << endl;
      cin.get();
      cin.get();
      return(0);
      };
    else if (X==3);
      {
      //Multiplication
      
      cout << " " << endl;
      cout << "Multiplication" << endl;
      cout << "X*Y=Z" << endl;
      cout << "What is X?" << endl;
      cin >> x2;
      cout << "What is Y?" << endl;
      cin >> y3;
      double z2 (x2*y3);
      cout << "Z equals to " << z2 << endl;
      cin.get();
      cin.get();
      return(0);
      };
    else if (X==4);
      {
      //Division
      
      cout << " " << endl;
      cout << "Division" << endl;
      cout << "X/Y=Z" << endl;
      cout << "What is X?" << endl;
      cin >> x3;
      cout << "What is Y?" << endl;
      cin >> y4
      double z3 (x3/y4);
      cout << "Z equals to " << z3 << endl;
      cin.get();
      cin.get();
      return(0);
      };
    else if (X==5); 
      {
      //Simple Interest Calculator
      
      cout << " " << endl;
      cout << "Interest Calculator-Simple" << endl;
      cout << "What is the Principle? ($)" <<endl;
      cin >> principle;
      cout << "What is the Rate? (%)" << endl;
      cin >> ratep;
      double rate = (1+(ratep/100));
      cout << "What is the rate of time? (Years)" << endl;
      cin >> years;
      cout << "The New Value of $" << principle << " at " << ratep << "% after " << years << " years:" << endl;
      cout << principle*rate*years << endl;
      cin.get();
      cin.get();
      return(0);
      };
    else if (X==6);
      {
      //Credits
       
      cout << " " << endl;
      cout << "Mathematics" << endl;
      cout << "(c) 2010-2011" << endl;
      cin.get();
      cin.get();
      return(0);  
      };
    else if (X==7);
      {
      //Exit
      
      cout << "Terminating Program" << endl;
      cout << "Mathematics" << endl;
      cout << "(c) 2010-2011" << endl;
      cin.get();
      cin.get();
      return(0);
      };
    };
    I Appreciate the HELP!
    Last edited by Shodai100; 09-08-2010 at 06:35 PM. Reason: Forgot to Tell Code Error

  2. #2
    Registered User nepper271's Avatar
    Join Date
    Jan 2008
    Location
    Brazil
    Posts
    50
    The correct syntax is
    Code:
    if (exp) {
    
    } else if (exp2) {
    
    }
    You are doing
    Code:
    if (exp); {
    
    }; else if (exp2); {
    
    }
    That's what I saw so far, aside from the programming design and logic.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    2

    Thank you so much!

    Thank you so much! My program now works. may i ask, do you know how i could make the program return back to the main menu after completing the set task?

    again, thank you so much!

  4. #4
    Registered User nepper271's Avatar
    Join Date
    Jan 2008
    Location
    Brazil
    Posts
    50
    Try something like
    Code:
    X = 0
    while (X != 7) {
      X = call_menu()
    }
    i.e. put everything inside a while whose stopping condition is the quit entry.

    Hope this helps.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Location
    pkr
    Posts
    32
    I always used switch case while doing these and it really makes it real easy.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Also, global variables are bad. Move all your variables into the main function.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,785
    And you really don't need to declare all variables at top. Declare them as locally as possible instead. If you're inputting two numbers, declare them just before reading them. It reduces clutter.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner programmer
    By Kool4School in forum C Programming
    Replies: 16
    Last Post: 11-24-2008, 12:16 PM
  2. [C] - String Manipulation {Beginner Programmer}
    By INFERNO2K in forum C Programming
    Replies: 14
    Last Post: 05-21-2005, 11:34 AM
  3. Beginner Programmer
    By silverjump in forum C++ Programming
    Replies: 2
    Last Post: 04-04-2005, 06:03 PM
  4. Beginner c++ programmer looking for suggestions....
    By Sheshi in forum C++ Programming
    Replies: 6
    Last Post: 03-08-2003, 03:38 PM
  5. Any beginner C++ programmer wants to.....
    By incognito in forum C++ Programming
    Replies: 5
    Last Post: 12-06-2001, 07:15 AM

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