Help in C++

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Allan

    Help in C++

    Ok, I got some good advise. I re-wrote the program and now I'm down to only
    1 error. Below is my code and my error. Any ideas what it is?
    #include <iostream>

    using std::cout;
    using std::cin;
    using std::endl;


    // Function main begins program execution

    int main()

    {

    int tempinfah; //temp in Fahrenheit
    int tempincel; //temp in Celsius

    cout<<"enter temperature in Fahrenheit\n";//Prompting for user input
    cin>>tempinfah; //user input

    tempincel = (tempinfah-32)*(5/9)

    cout<<"Temperat ure in Celsius" << endl;

    return 0;


    }
    error: C:\Program Files\Microsoft Visual Studio\MyProjec ts\Assignment
    11\11.cpp(24) : error C2146: syntax error : missing ';' before identifier
    'cout'
    Error executing cl.exe.

    Assignment 11.exe - 1 error(s), 0 warning(s)


  • Mike Wahler

    #2
    Re: Help in C++


    "Allan" <allan118@cfl.r r.com> wrote in message
    news:NEpbb.1848 9$kg.4819@twist er.tampabay.rr. com...[color=blue]
    > Ok, I got some good advise. I re-wrote the program and now I'm down to[/color]
    only[color=blue]
    > 1 error. Below is my code and my error. Any ideas what it is?
    > #include <iostream>
    >
    > using std::cout;
    > using std::cin;
    > using std::endl;
    >
    >
    > // Function main begins program execution
    >
    > int main()
    >
    > {
    >
    > int tempinfah; //temp in Fahrenheit
    > int tempincel; //temp in Celsius
    >
    > cout<<"enter temperature in Fahrenheit\n";//Prompting for user input
    > cin>>tempinfah; //user input
    >
    > tempincel = (tempinfah-32)*(5/9)[/color]

    Exactly what the message says, you're missing a semicolon
    here, which is just before 'cout' below.
    [color=blue]
    >
    > cout<<"Temperat ure in Celsius" << endl;
    >
    > return 0;[/color]

    Also note that you're still truncating your results
    as others have already pointed out.
    [color=blue]
    >
    >
    > }
    > error: C:\Program Files\Microsoft Visual Studio\MyProjec ts\Assignment
    > 11\11.cpp(24) : error C2146: syntax error : missing ';' before identifier
    > 'cout'
    > Error executing cl.exe.
    >
    > Assignment 11.exe - 1 error(s), 0 warning(s)[/color]

    -Mike


    Comment

    • Jacek Dziedzic

      #3
      Re: Help in C++

      "Allan" <allan118@cfl.r r.com> wrote in message news:NEpbb.1848 9
      $kg.4819@twiste r.tampabay.rr.c om...[color=blue]
      > Ok, I got some good advise. I re-wrote the program and now
      > I'm down to only 1 error. Below is my code and my error.
      > Any ideas what it is?
      > [...]
      > error C2146: syntax error : missing ';' before identifier
      > 'cout'[/color]

      Well, the compiler told you, plain and simple. You're
      missing a semicolon before 'cout', or in other words after

      tempincel = (tempinfah-32)*(5/9)

      If you fix it your program will compile, but it won't work
      as expected as you have two more errors left.

      One is that (5/9) evaluates to zero, because your variables
      are integer (ints) and hence cannot represent 5/9.

      The other is that you neglect to output your calculated
      result. Duh!

      HTH,
      - J.

      PS. Get a textbook!


      Comment

      • Gianni Mariani

        #4
        Re: Help in C++

        Allan wrote:[color=blue]
        > Ok, I got some good advise. I re-wrote the program and now I'm down to only
        > 1 error. Below is my code and my error. Any ideas what it is?
        > #include <iostream>
        >
        > using std::cout;
        > using std::cin;
        > using std::endl;
        >
        >
        > // Function main begins program execution
        >
        > int main()
        >
        > {
        >
        > int tempinfah; //temp in Fahrenheit
        > int tempincel; //temp in Celsius
        >
        > cout<<"enter temperature in Fahrenheit\n";//Prompting for user input
        > cin>>tempinfah; //user input
        >
        > tempincel = (tempinfah-32)*(5/9)
        >
        > cout<<"Temperat ure in Celsius" << endl;
        >
        > return 0;
        >
        >
        > }
        > error: C:\Program Files\Microsoft Visual Studio\MyProjec ts\Assignment
        > 11\11.cpp(24) : error C2146: syntax error : missing ';' before identifier
        > 'cout'
        > Error executing cl.exe.
        >
        > Assignment 11.exe - 1 error(s), 0 warning(s)[/color]


        Cool, like the other posters said, you'll need that ';' after the
        "tempincel = (tempinfah-32)*(5/9)".

        In C++, every statement is followed by a ';'. Not to be confused with
        statements grouped by "{" and "}" which is a compound statement.

        The next problem is the "int", you have 2 alternatives in C and C++,
        "float" and "double". So, instead of using "int" for temperatures, use
        double. But you still have a problem because "5" and "9" are integers
        and (5/9) will still be zero - but (5.0/9.0) is a double (in fact, so is
        (5/9.), (5./9), (5e0/9), (5*1./9) - you'll need to study type promotion
        to get this, suffice to say (5.0/9.0) is easy and unambiguius to read so
        it's the best choice.

        Last thing.

        You'll need to place "<< tempincel" somewhere interesting.

        If you have any other issues, go ahead an respond with a copy of what
        you've got after you make the changes. It's cake !


        Comment

        • Allan

          #5
          Re: Help in C++

          How do I output my calculated result? Like I said earlier, I got a late
          start in this class, and I do have a textbook.
          This assignment is due tomorrow night and I'm beating my head against the
          wall trying to catch up. This is just one of three programs I have to write.
          "Jacek Dziedzic" <jacek-NOSPAM-@janowo.net> wrote in message
          news:13949-1064184467@jano wo.net...[color=blue]
          > "Allan" <allan118@cfl.r r.com> wrote in message news:NEpbb.1848 9
          > $kg.4819@twiste r.tampabay.rr.c om...[color=green]
          > > Ok, I got some good advise. I re-wrote the program and now
          > > I'm down to only 1 error. Below is my code and my error.
          > > Any ideas what it is?
          > > [...]
          > > error C2146: syntax error : missing ';' before identifier
          > > 'cout'[/color]
          >
          > Well, the compiler told you, plain and simple. You're
          > missing a semicolon before 'cout', or in other words after
          >
          > tempincel = (tempinfah-32)*(5/9)
          >
          > If you fix it your program will compile, but it won't work
          > as expected as you have two more errors left.
          >
          > One is that (5/9) evaluates to zero, because your variables
          > are integer (ints) and hence cannot represent 5/9.
          >
          > The other is that you neglect to output your calculated
          > result. Duh!
          >
          > HTH,
          > - J.
          >
          > PS. Get a textbook!
          >
          >
          >[/color]


          Comment

          • Kevin Goodsell

            #6
            Re: Help in C++

            Allan wrote:[color=blue]
            > How do I output my calculated result? Like I said earlier, I got a late
            > start in this class, and I do have a textbook.
            > This assignment is due tomorrow night and I'm beating my head against the
            > wall trying to catch up. This is just one of three programs I have to write.[/color]

            Please don't top-post. Read section 5 of the FAQ for posting guidelines.



            Generally, you can output things like this:

            cout << a;

            If you want to output multiple things, you can do this:

            cout << a << b << c;

            If you want some space between the things you output, do this:

            cout << a << ' ' << b << ' ' << c;

            Or maybe this:

            cout << a << ", " << b << ", " << c;

            It's a good idea to terminate all output lines with a newline. One way
            to do this is with 'endl', but be aware that it also flushes the output
            stream, which means if forces buffered output to go to it's final
            destination. Generally this may be expensive, which is why buffering is
            used. In practice, it will make no difference in the kind of programs
            you will write for your class.

            cout << a << ", " << b << ", " << c << endl;

            -Kevin
            --
            My email address is valid, but changes periodically.
            To contact me please use the address from a recent posting.

            Comment

            • jeffc

              #7
              Re: Help in C++


              "Allan" <allan118@cfl.r r.com> wrote in message
              news:NEpbb.1848 9$kg.4819@twist er.tampabay.rr. com...[color=blue]
              > Ok, I got some good advise. I re-wrote the program and now I'm down to[/color]
              only[color=blue]
              > 1 error. Below is my code and my error. Any ideas what it is?
              > #include <iostream>
              >
              > using std::cout;
              > using std::cin;
              > using std::endl;
              >
              >
              > // Function main begins program execution
              >
              > int main()
              >
              > {
              >
              > int tempinfah; //temp in Fahrenheit
              > int tempincel; //temp in Celsius
              >
              > cout<<"enter temperature in Fahrenheit\n";//Prompting for user input
              > cin>>tempinfah; //user input
              >
              > tempincel = (tempinfah-32)*(5/9)
              >
              > cout<<"Temperat ure in Celsius" << endl;
              >
              > return 0;
              > }
              > error: C:\Program Files\Microsoft Visual Studio\MyProjec ts\Assignment
              > 11\11.cpp(24) : error C2146: syntax error : missing ';' before identifier
              > 'cout'[/color]

              I'm going to go way out on a limb here and say that there's a missing ';'
              before cout.


              Comment

              Working...