Little converter problem

This is a discussion on Little converter problem within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream.h> using namespace std; void gl(); void lg(); void mk(); void km(); void pk(); void kp(); int a; ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    4

    Little converter problem

    Code:
    #include <iostream.h>
    
    using namespace std;
    void gl();
    void lg();
    void mk();
    void km();
    void pk();
    void kp();
    
    
    int a;
    
    int main()
    {
        cout << "Choose the converter you want (1-6): \n";
        cout << "1: Gallons to liters. \n";
        cout << "2: Liters to gallons. \n";
        cout << "3: Miles to kilometers. \n";
        cout << "4: Kilometers to miles. \n";
        cout << "5: Pounds to kilograms. \n";
        cout << "6: Kilograms to pounds. \n";
        cout << "\n";
        cin >> a;
        
        switch(a){
        case 1: gl();
        case 2: lg();
        case 3: mk();
        case 4: km();
        case 5: pk();
        case 6: kp();
                }
    }
    // Gallons to liter converter
    
    void gl()
    {
    float liters;
    float gallons;
    
        {
              
              cout << "Input gallons. \n";
              cin >> gallons;
         liters = gallons * 3.7854118;
              cout << "Liters:" << liters;
         }
         
    }
    
    // Kilograms to pounds converter
    
    void kp()
    
    float lbs;
    float kg;
    
    {
        {
              
              cout << "Input kilograms. \n";
              cin >> kg;
         float lbs = kg * 2.20462262;
              cout << "Pounds:" << lbs;
         }
         
    }
    
    // Kilometers to miles converter
    
    void km()
    
    float kilometers;
    float miles;
    
    
    {
        {
              
              cout << "Input kilometers. \n";
              cin >> kilometers;
         miles = kilometers * 1.609344;
              cout << "Miles:" << miles;
         }
         
    }
    
    // Liters to gallons converter
    
    void lg()
    
    float liters;
    float gallons;
    
    {
        {
              
              cout << "Input liters. \n";
              cin >> liters;
         gallons = liters * 0.264172051;
              cout << "Gallons:" << gallons;
         }
         
    }
    
    // Pounds to kilograms converter
    
    void pk()
    
    float lbs;
    float kg;
    
    {
        {
              
              cout << "Input pounds. \n";
              cin >> lbs;
         kg = lbs * 0.45359237;
              cout << "Kilograms:" << kg;
         }
         
    }
    
    // Miles to kilometers converter
    
    void mk()
    
    float kilometers;
    float miles;
    
    {
        {
              
              cout << "Input miles. \n";
              cin >> miles;
         kilometers = miles * 0.621371192;
              cout << "Kilometers:" << kilometers;
         }
         
    }
    I keep getting syntax errors every time compile it. Each converter works alone, but I cant seem to get them to work together.
    Last edited by hpotter114; 01-19-2005 at 01:53 PM.

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Post your styntax errors.

    edit:

    First of all, remove the .h from iostream, and add a "break;" statement after each case in your switch.
    Last edited by sean; 01-19-2005 at 02:03 PM.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    4
    Oops, sorry. Here they are:

    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp: In
    function `void kp()':
    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp:56: error: syntax
    error before `float'
    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp:59: error: syntax
    error before `{' token
    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp:63: error: syntax
    error before `>>' token
    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp:64: error: redefinition
    of `float lbs'

    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp:56: error: `
    float lbs' previously declared here
    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp:65: error: syntax
    error before `<<' token
    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp: In
    function `void km()':
    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp:74: error: syntax
    error before `float'
    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp:78: error: syntax
    error before `{' token
    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp:82: error: syntax
    error before `>>' token
    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp:83: error: ISO
    C++ forbids declaration of `miles' with no type
    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp:83: error: conflicting
    types for `int miles'
    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp:75: error: previous
    declaration as `float miles'

    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp:84: error: syntax
    error before `<<' token
    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp: In
    function `void lg()':
    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp:93: error: syntax
    error before `float'
    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp:96: error: syntax
    error before `{' token
    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp:100: error: syntax
    error before `>>' token
    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp:101: error: ISO
    C++ forbids declaration of `gallons' with no type
    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp:101: error: conflicting
    types for `int gallons'
    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp:94: error: previous
    declaration as `float gallons'
    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp:102: error: syntax
    error before `<<' token
    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp: In
    function `void pk()':
    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp:111: error: syntax
    error before `float'
    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp:111: error: redefinition
    of `float lbs'
    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp:64: error: `
    float lbs' previously declared here
    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp:112: error: redefinition
    of `float kg'

    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp:57: error: `
    float kg' previously declared here
    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp:114: error: syntax
    error before `{' token
    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp:118: error: syntax
    error before `>>' token
    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp:119: error: ISO
    C++ forbids declaration of `kg' with no type
    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp:119: error: conflicting
    types for `int kg'
    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp:112: error: previous
    declaration as `float kg'
    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp:120: error: syntax
    error before `<<' token
    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp: In
    function `void mk()':
    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp:129: error: syntax
    error before `float'
    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp:129: error: redefinition
    of `float kilometers'

    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp:74: error: `
    float kilometers' previously declared here
    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp:130: error: type
    mismatch with previous external decl
    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp:83: error: previous
    external decl of `int miles'
    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp:132: error: syntax
    error before `{' token
    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp:136: error: syntax
    error before `>>' token
    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp:137: error: ISO
    C++ forbids declaration of `kilometers' with no type
    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp:137: error: conflicting
    types for `int kilometers'
    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp:129: error: previous
    declaration as `float kilometers'
    C:/Documents and Settings/Administrator/My Documents/conversions/converter.cpp:138: error: syntax
    error before `<<' token

    EDIT: Adding break and taking away the .h fixed the errors about the "not in switch statement"
    Last edited by hpotter114; 01-19-2005 at 02:07 PM.

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    declare all your variables inside the appropriate functions. If you're going to declare them globally, you have to do all of them before you define main, and none of them need to to be declared globally. Always declare stuff as locally as possible.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    4
    I just realized what I did wrong. I misplaced the {. I put it after the variable instead of before...

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >void kp()

    >float lbs;
    >float kg;

    >{

    The beginning function brace goes before any local declarations:
    Code:
    void kp()
    {
    float lbs;
    float kg;
    You did that on just about every function.
    Last edited by swoopy; 01-19-2005 at 02:19 PM.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    4
    I know. I changed that and it works fine now! Thanks guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Replies: 4
    Last Post: 03-12-2006, 01:17 PM

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