Thread: expression must be of integral type in function main()

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    2

    expression must be of integral type in function main()

    Code:
    //uppgift.cpp
    #include <iostream>
    using namespace std;
    #include <float.h>
    
    int main ()
    {
       float car, tal1, tal2, tal3;
    
       cout <<"*******************************\n";
       cout <<"*Bensinförbruknings-kalkylator*\n";
       cout <<"*******************************\n\n";
    
       cout <<"Bekräfta med att trycka Enter\n\n";
    
       cout << "\nInmatning - Antal körda mil : ";
       cin >> tal1;
       
       cout << "\nInmatning - Antal tankade liter : ";
       cin >> tal2;
    
       cout <<"\nVal av bil\n\n";
       cout << "(1)Volvo\n";
       cout << "(2)Opel\n"; 
       cout << "(3)Saab\n";
       cout << "Välj bil genom att ange en siffra mellan 1-3 : ";
       cin >> car;
       switch (car)
       {
          case 1:
       cout << "Mil\tBil\tLiter\tFörbrukning\n";
       cout << "----\t----\t------\t------------\n";
        cout << "" << tal1 << " \tVolvo\t " << tal2 
             << "      " << (tal2/tal1) << "\n";
       cout <<"\n\n\n*******************************\n";
       cout <<"*    BilligaBilar AB 2006     *\n";
       cout <<"*   Skapat av Patrik Ljung    *\n";
       cout <<"*******************************\n\n";
             break;
          case 2:
       cout << "Mil\tBil\tLiter\tFörbrukning\n";
       cout << "----\t----\t------\t------------\n";
        cout << "" << tal1 << " \tOpel\t " << tal2 
             << "      " << (tal2/tal1) << "\n";
       cout <<"\n\n\n*******************************\n";
       cout <<"*    BilligaBilar AB 2006     *\n";
       cout <<"*   Skapat av Patrik Ljung    *\n";
       cout <<"*******************************\n\n";
             break;
          case 3:
       cout << "Mil\tBil\tLiter\tFörbrukning\n";
       cout << "----\t----\t------\t------------\n";
        cout << "" << tal1 << " \tOpel\t " << tal2 
             << "      " << (tal2/tal1) << "\n";
       cout <<"\n\n\n*******************************\n";
       cout <<"*    BilligaBilar AB 2006     *\n";
       cout <<"*   Skapat av Patrik Ljung    *\n";
       cout <<"*******************************\n\n";
       
        }
        return 0;
    }
    ok so this is my code and i cant get the switch function to work (dont mind the langauge of the text its in swedish ) but i cant figure out how to get the switch function to work

    thank

  2. #2
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    you can't use a float as an argument in a switch. it has to be an integral type; integral as part of the integer family of types. int, char, long, etc.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Din variabel "car" är en flyttals-variabel. Det går inte att använda flyttal i switch-satser.

    For those who do not understand Swedish:
    The variable "car" is a floating point value. It is not possible to use floating point for switch-statements.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    2
    ok ty i figured it out now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion of pointers to functions
    By hzmonte in forum C Programming
    Replies: 0
    Last Post: 01-20-2009, 01:56 AM
  2. Little Array Difficulty
    By G4B3 in forum C Programming
    Replies: 16
    Last Post: 03-19-2008, 12:59 AM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. Replies: 6
    Last Post: 04-21-2006, 08:49 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM