Find the answer to your Linux question:
Page 1 of 3 1 2 3 LastLast
Results 1 to 10 of 21
well im just starting to learn C++, and the book im going through gives me this program Code: #include <iostream> void simon&#40;int&#41;; int main&#40;&#41; &#123; using namespace std; simon&#40;3&#41;; cout ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2005
    Posts
    98

    C++ compiling w/ g++


    well im just starting to learn C++, and the book im going through gives me this program
    Code:
    #include <iostream>
    void simon&#40;int&#41;;
    
    int main&#40;&#41;
    &#123;
            using namespace std;
            simon&#40;3&#41;;
            cout << "pick a number"
            int count;
            cin >> count;
            simon&#40;count&#41;;
            cout << "Done" << endl;
            return 0;
    &#125;
    void simon&#40;int n&#41;
    &#123;
            using namespace std;
            cout << " Simon says touch your toes" << n << "times." << endl;
    &#125;
    when i try and compile that w/ g++ i get this error
    Code:
    UDfunc1.C&#58; In function `int main&#40;&#41;'&#58;
    UDfunc1.C&#58;9&#58; error&#58; parse error before `int'
    UDfunc1.C&#58;10&#58; error&#58; no match for 'operator>>' in 'std&#58;&#58;cin >> std&#58;&#58;count'
    /usr/include/c++/3.3.4/bits/istream.tcc&#58;86&#58; error&#58; candidates are&#58;
       std&#58;&#58;basic_istream<_CharT, _Traits>& std&#58;&#58;basic_istream<_CharT,
       _Traits>&#58;&#58;operator>>&#40;std&#58;&#58;basic_istream<_CharT,
       _Traits>&&#40;*&#41;&#40;std&#58;&#58;basic_istream<_CharT, _Traits>&&#41;&#41; &#91;with _CharT = char,
       _Traits = std&#58;&#58;char_traits<char>&#93;
    /usr/include/c++/3.3.4/bits/istream.tcc&#58;92&#58; error&#58;
       std&#58;&#58;basic_istream<_CharT, _Traits>& std&#58;&#58;basic_istream<_CharT,
       _Traits>&#58;&#58;operator>>&#40;std&#58;&#58;basic_ios<_CharT,
       _Traits>&&#40;*&#41;&#40;std&#58;&#58;basic_ios<_CharT, _Traits>&&#41;&#41; &#91;with _CharT = char, _Traits
       = std&#58;&#58;char_traits<char>&#93;
    /usr/include/c++/3.3.4/bits/istream.tcc&#58;101&#58; error&#58;
       std&#58;&#58;basic_istream<_CharT, _Traits>& std&#58;&#58;basic_istream<_CharT,
       _Traits>&#58;&#58;operator>>&#40;std&#58;&#58;ios_base&&#40;*&#41;&#40;std&#58;&#58;ios_base&&#41;&#41; &#91;with _CharT = char,
       _Traits = std&#58;&#58;char_traits<char>&#93;
    /usr/include/c++/3.3.4/bits/istream.tcc&#58;110&#58; error&#58;
       std&#58;&#58;basic_istream<_CharT, _Traits>& std&#58;&#58;basic_istream<_CharT,
       _Traits>&#58;&#58;operator>>&#40;bool&&#41; &#91;with _CharT = char, _Traits =
       std&#58;&#58;char_traits<char>&#93;
    /usr/include/c++/3.3.4/bits/istream.tcc&#58;133&#58; error&#58;
       std&#58;&#58;basic_istream<_CharT, _Traits>& std&#58;&#58;basic_istream<_CharT,
       _Traits>&#58;&#58;operator>>&#40;short int&&#41; &#91;with _CharT = char, _Traits =
       std&#58;&#58;char_traits<char>&#93;
    /usr/include/c++/3.3.4/bits/istream.tcc&#58;165&#58; error&#58;
       std&#58;&#58;basic_istream<_CharT, _Traits>& std&#58;&#58;basic_istream<_CharT,
       _Traits>&#58;&#58;operator>>&#40;short unsigned int&&#41; &#91;with _CharT = char, _Traits =
       std&#58;&#58;char_traits<char>&#93;
    /usr/include/c++/3.3.4/bits/istream.tcc&#58;188&#58; error&#58;
       std&#58;&#58;basic_istream<_CharT, _Traits>& std&#58;&#58;basic_istream<_CharT,
       _Traits>&#58;&#58;operator>>&#40;int&&#41; &#91;with _CharT = char, _Traits =
       std&#58;&#58;char_traits<char>&#93;
    /usr/include/c++/3.3.4/bits/istream.tcc&#58;220&#58; error&#58;
       std&#58;&#58;basic_istream<_CharT, _Traits>& std&#58;&#58;basic_istream<_CharT,
       _Traits>&#58;&#58;operator>>&#40;unsigned int&&#41; &#91;with _CharT = char, _Traits =
       std&#58;&#58;char_traits<char>&#93;
    /usr/include/c++/3.3.4/bits/istream.tcc&#58;243&#58; error&#58;
       std&#58;&#58;basic_istream<_CharT, _Traits>& std&#58;&#58;basic_istream<_CharT,
       _Traits>&#58;&#58;operator>>&#40;long int&&#41; &#91;with _CharT = char, _Traits =
       std&#58;&#58;char_traits<char>&#93;
    /usr/include/c++/3.3.4/bits/istream.tcc&#58;266&#58; error&#58;
       std&#58;&#58;basic_istream<_CharT, _Traits>& std&#58;&#58;basic_istream<_CharT,
       _Traits>&#58;&#58;operator>>&#40;long unsigned int&&#41; &#91;with _CharT = char, _Traits =
       std&#58;&#58;char_traits<char>&#93;
    /usr/include/c++/3.3.4/bits/istream.tcc&#58;290&#58; error&#58;
       std&#58;&#58;basic_istream<_CharT, _Traits>& std&#58;&#58;basic_istream<_CharT,
       _Traits>&#58;&#58;operator>>&#40;long long int&&#41; &#91;with _CharT = char, _Traits =
       std&#58;&#58;char_traits<char>&#93;
    /usr/include/c++/3.3.4/bits/istream.tcc&#58;313&#58; error&#58;
       std&#58;&#58;basic_istream<_CharT, _Traits>& std&#58;&#58;basic_istream<_CharT,
       _Traits>&#58;&#58;operator>>&#40;long long unsigned int&&#41; &#91;with _CharT = char, _Traits =
       std&#58;&#58;char_traits<char>&#93;
    /usr/include/c++/3.3.4/bits/istream.tcc&#58;337&#58; error&#58;
       std&#58;&#58;basic_istream<_CharT, _Traits>& std&#58;&#58;basic_istream<_CharT,
       _Traits>&#58;&#58;operator>>&#40;float&&#41; &#91;with _CharT = char, _Traits =
       std&#58;&#58;char_traits<char>&#93;
    /usr/include/c++/3.3.4/bits/istream.tcc&#58;360&#58; error&#58;
       std&#58;&#58;basic_istream<_CharT, _Traits>& std&#58;&#58;basic_istream<_CharT,
       _Traits>&#58;&#58;operator>>&#40;double&&#41; &#91;with _CharT = char, _Traits =
       std&#58;&#58;char_traits<char>&#93;
    /usr/include/c++/3.3.4/bits/istream.tcc&#58;383&#58; error&#58;
       std&#58;&#58;basic_istream<_CharT, _Traits>& std&#58;&#58;basic_istream<_CharT,
       _Traits>&#58;&#58;operator>>&#40;long double&&#41; &#91;with _CharT = char, _Traits =
       std&#58;&#58;char_traits<char>&#93;
    /usr/include/c++/3.3.4/bits/istream.tcc&#58;406&#58; error&#58;
       std&#58;&#58;basic_istream<_CharT, _Traits>& std&#58;&#58;basic_istream<_CharT,
       _Traits>&#58;&#58;operator>>&#40;void*&&#41; &#91;with _CharT = char, _Traits =
       std&#58;&#58;char_traits<char>&#93;
    /usr/include/c++/3.3.4/bits/istream.tcc&#58;429&#58; error&#58;
       std&#58;&#58;basic_istream<_CharT, _Traits>& std&#58;&#58;basic_istream<_CharT,
       _Traits>&#58;&#58;operator>>&#40;std&#58;&#58;basic_streambuf<_CharT, _Traits>*&#41; &#91;with _CharT =
       char, _Traits = std&#58;&#58;char_traits<char>&#93;
    /usr/include/c++/3.3.4/bits/istream.tcc&#58;921&#58; error&#58;
       std&#58;&#58;basic_istream<_CharT, _Traits>&
       std&#58;&#58;operator>>&#40;std&#58;&#58;basic_istream<_CharT, _Traits>&, _CharT&&#41; &#91;with _CharT
       = char, _Traits = std&#58;&#58;char_traits<char>&#93;
    /usr/include/c++/3.3.4/istream&#58;644&#58; error&#58;
       std&#58;&#58;basic_istream<char, _Traits>& std&#58;&#58;operator>>&#40;std&#58;&#58;basic_istream<char,
       _Traits>&, unsigned char&&#41; &#91;with _Traits = std&#58;&#58;char_traits<char>&#93;
    /usr/include/c++/3.3.4/istream&#58;649&#58; error&#58;
       std&#58;&#58;basic_istream<char, _Traits>& std&#58;&#58;operator>>&#40;std&#58;&#58;basic_istream<char,
       _Traits>&, signed char&&#41; &#91;with _Traits = std&#58;&#58;char_traits<char>&#93;
    /usr/include/c++/3.3.4/bits/istream.tcc&#58;946&#58; error&#58;
       std&#58;&#58;basic_istream<_CharT, _Traits>&
       std&#58;&#58;operator>>&#40;std&#58;&#58;basic_istream<_CharT, _Traits>&, _CharT*&#41; &#91;with _CharT
       = char, _Traits = std&#58;&#58;char_traits<char>&#93;
    /usr/include/c++/3.3.4/istream&#58;685&#58; error&#58;
       std&#58;&#58;basic_istream<char, _Traits>& std&#58;&#58;operator>>&#40;std&#58;&#58;basic_istream<char,
       _Traits>&, unsigned char*&#41; &#91;with _Traits = std&#58;&#58;char_traits<char>&#93;
    /usr/include/c++/3.3.4/istream&#58;690&#58; error&#58;
       std&#58;&#58;basic_istream<char, _Traits>& std&#58;&#58;operator>>&#40;std&#58;&#58;basic_istream<char,
       _Traits>&, signed char*&#41; &#91;with _Traits = std&#58;&#58;char_traits<char>&#93;
    UDfunc1.C&#58;11&#58; error&#58; cannot resolve overloaded function `count' based on
       conversion to type `int'
    UDfunc1.C&#58; In function `void simon&#40;int&#41;'&#58;
    UDfunc1.C&#58;18&#58; error&#58; parse error before `<<' token
    i have no idea where i messed up in the code it looks the same as in the book
    (this section of the book deals w/ user defined functions, which would be simon in this case)

  2. #2
    Linux User nalg0rath's Avatar
    Join Date
    Sep 2004
    Location
    Stockholm
    Posts
    303
    #include <iostream>
    void simon(int);

    using namespace std;


    int main()
    {
    using namespace std;
    simon(3);
    cout << "pick a number"; // <- semicolon
    int count;
    cin >> count;
    simon(count);
    cout << "Done" << endl;
    return 0;
    }
    void simon(int n)
    {
    using namespace std;
    cout << " Simon says touch your toes" << n << "times." << endl;
    }


    ?

  3. #3
    Just Joined!
    Join Date
    Mar 2005
    Posts
    98
    stupid book didnt put the "using namespace" in the right spot

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Mar 2005
    Posts
    98
    alright sry i have one more problem
    Code:
    include <iostream>
    int fire&#40;int&#41;;
    
    using namespace std;
    int main &#40;&#41;
    &#123;
            int dist;
            cout << "enter distance in ft";
            cin >> dist;
            int furlongs = fire&#40;dist&#41;
            cout << dist << " ft. = ";
            cout << furlongs << "furlongs " << endl;
            return 0;
    &#125;
    
    int fire &#40;int hi&#41;
    &#123;
            return 220*3* hi;
    &#125;
    whenever i try and compile that i get this error
    Code:
    furlongs.C&#58; In function `int main&#40;&#41;'&#58;
    furlongs.C&#58;11&#58; error&#58; parse error before `<<' token
    and i have spent about a half an hour looking for the error but i cant find it

  6. #5
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    Code:
    include <iostream>
    int fire&#40;int&#41;;
    
    using namespace std;
    int main &#40;&#41;
    &#123;
            int dist;
            cout << "enter distance in ft";
            cin >> dist;
            int furlongs = fire&#40;dist&#41;;   // <-- ADD THE SEMICOLON HERE!!
            cout << dist << " ft. = ";
            cout << furlongs << "furlongs " << endl;
            return 0;
    &#125;
    
    int fire &#40;int hi&#41;
    &#123;
            return 220*3* hi;
    &#125;

    Remember: every line of code needs a semicolon on the end.

  7. #6
    Linux Newbie
    Join Date
    Nov 2004
    Location
    New York
    Posts
    150
    Whenever you see a long list of errors, always look at the first one, as a single typo can cause countless cascading problems further down. When you have a syntax error, it's most often a semicolon issue for people newer to the language. Even if the error isn't all that helpful, look at the line where it's flagging an error, as well as the line immediately preceding it, and see if you can find anything wrong with the syntax.

    By the way, anyone know whether it is in fact legal to use the using declaration (directive?) inside function scope?
    \"Nifty News Fifty: When news breaks, we give you the pieces.\" - Sluggy Freelance

  8. #7
    Just Joined!
    Join Date
    Mar 2005
    Posts
    98
    lol ok i got another, well im going through the C++ primer plus book, and it gives me assignments to do, and i cant figure how to do one of them it says: "Write a program that asks the user to enter an hour value and a minute value. The main() function should then pass these two values to a type void function that displays the two values in the format shown inthe fallowing sample run:
    Enter the number of hours: 9
    Enter the number of minutes: 38
    Time: 9:38
    the closest i got it turned out like this:
    Enter a number in hours
    5
    enter a number of minutes
    06
    the time is5
    the time is6
    as you can see time is repeated and the 0 isnt showing up w/ the 6

  9. #8
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    Well, post up your code and we can take a looksie.

    The one thing that I CAN tell you is that your int of 06 will appear as 6. This is because, in an int, 06 = 6. You may want to use a string, which would take this as a literal, and use it as such. Since you're not running any operations on it, this should work.

  10. #9
    Just Joined!
    Join Date
    Mar 2005
    Posts
    98
    here is the code i used:
    Code:
    #include <iostream>
    using namespace std;
    void  hi&#40;int&#41;;
    main&#40;&#41;
    &#123;
            int cookies;
            cout << "Enter a number in hours" << endl;
            cin >>  cookies;
            int oreo;
            cout << "enter a number in hours" << endl;
            cin >> oreo;
            hi&#40;cookies&#41;;
            hi&#40;oreo&#41;;
           return 0;
    &#125;
    
    void  hi&#40;int n&#41;
    
    &#123;
            cout << "the time is" << n << endl;
    &#125;
    and this is the result i received:
    Code:
    Enter a number in hours
    5
    enter a number in hours
    06
    the time is5
    the time is6
    and the instructions read:
    "Write a program that asks the user to enter an hour value and a minute value. The main() function should then pass these two values to a type void function that displays the two values in the format shown inthe fallowing sample run:
    Enter the number of hours: 9
    Enter the number of minutes: 38
    Time: 9:38"

  11. #10
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    Aha, I see the issues.

    First off, the "06" thing. As long as that is inputted as an int, it will be displayed as an int (that is, as "6"). To grab the literal value, you must use either a string or, if you don't know C++ strings, an array of characters (aka a C string).

    As for the issue with the output, let's examine this:

    Code:
    void  hi&#40;int n&#41;
    
    &#123;
            cout << "the time is" << n << endl;
    &#125;
    If I call hi(4), it will print "the time is 4". So you can't simply send call it twice with a single parameter each time.

    You need to somehow work with 2 values at once. Can you think of how to do that?

    If you'd like a bit of a code snippet, ask, and I'll provide.

Page 1 of 3 1 2 3 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •