CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Apr 2010
    Posts
    6

    help, whats wrong with this code?

    i am a noob just trying to learn c++ and where i am learning theres an exam for the first lesson i just did but from all exercises i just cant do this one, can you help me?

    this is the code, there are several things wrong with it:

    #include <iostream> #include <string>

    int mian ()
    {
    string str = "Hello world!"
    cout << str << endl;
    cout << float x = 5.0f * str << end;
    int 65num = 65;
    cout << " 65num = " << 65num << endl;

    }
    what i could solve become this

    #include <iostream>
    #include <string>

    using namespace std;
    int main ()
    {
    string str ;
    cout << "Hello world!";
    cin >> str;
    cout << endl;
    cout << float x = 5.0f * str << endl;
    int num65 = 65;
    cout << " 65num = " << 65num << endl;

    }

    i cant figure out the rest can someone help me here plz?

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: help, whats wrong with this code?

    Why not learn let the compiler help you?

    Type the code in, compile it and see what errors you get.

    That way, you can learn how the compile errors map to the errors in your code.

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: help, whats wrong with this code?

    I have no idea what's going on with that float, and I have no idea why you figured you should add a cin which was not previously in evidence. Perhaps a more complete description of the problem is required.

  4. #4
    Join Date
    Apr 2010
    Posts
    6

    Re: help, whats wrong with this code?

    thats the problem there is no more description about this i just got this code and it saied to solve the errors it got nothing else, i dont even know what this code is supose to do

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: help, whats wrong with this code?

    Quote Originally Posted by Driku View Post
    thats the problem there is no more description about this i just got this code and it saied to solve the errors it got nothing else, i dont even know what this code is supose to do
    Type the code in, compile it, and it will help you figure out the errors.

    Do you have access to a compiler like Visual C++? If not, let us know and we can give you a link to where you can download a free version.

  6. #6
    Join Date
    Apr 2010
    Posts
    6

    Re: help, whats wrong with this code?

    i do have visual c++ 2008, i will post the errors that i got

    the original code i got this

    1>------ Build started: Project: 1.6.1, Configuration: Debug Win32 ------
    1>Compiling...
    1>exercise.cpp
    1>c:\users\lai\documents\visual studio 2008\projects\1.6.1\1.6.1\exercise.cpp(1) : warning C4067: unexpected tokens following preprocessor directive - expected a newline
    1>c:\users\lai\documents\visual studio 2008\projects\1.6.1\1.6.1\exercise.cpp(5) : error C2065: 'string' : undeclared identifier
    1>c:\users\lai\documents\visual studio 2008\projects\1.6.1\1.6.1\exercise.cpp(5) : error C2146: syntax error : missing ';' before identifier 'str'
    1>c:\users\lai\documents\visual studio 2008\projects\1.6.1\1.6.1\exercise.cpp(5) : error C2065: 'str' : undeclared identifier
    1>c:\users\lai\documents\visual studio 2008\projects\1.6.1\1.6.1\exercise.cpp(6) : error C2146: syntax error : missing ';' before identifier 'cout'
    1>c:\users\lai\documents\visual studio 2008\projects\1.6.1\1.6.1\exercise.cpp(6) : error C2065: 'cout' : undeclared identifier
    1>c:\users\lai\documents\visual studio 2008\projects\1.6.1\1.6.1\exercise.cpp(6) : error C2065: 'str' : undeclared identifier
    1>c:\users\lai\documents\visual studio 2008\projects\1.6.1\1.6.1\exercise.cpp(6) : error C2065: 'endl' : undeclared identifier
    1>c:\users\lai\documents\visual studio 2008\projects\1.6.1\1.6.1\exercise.cpp(7) : error C2065: 'cout' : undeclared identifier
    1>c:\users\lai\documents\visual studio 2008\projects\1.6.1\1.6.1\exercise.cpp(7) : error C2062: type 'float' unexpected
    1>c:\users\lai\documents\visual studio 2008\projects\1.6.1\1.6.1\exercise.cpp(8) : error C2059: syntax error : 'bad suffix on number'
    1>c:\users\lai\documents\visual studio 2008\projects\1.6.1\1.6.1\exercise.cpp(8) : warning C4091: '' : ignored on left of 'int' when no variable is declared
    1>c:\users\lai\documents\visual studio 2008\projects\1.6.1\1.6.1\exercise.cpp(8) : error C2143: syntax error : missing ';' before 'constant'
    1>c:\users\lai\documents\visual studio 2008\projects\1.6.1\1.6.1\exercise.cpp(8) : error C2146: syntax error : missing ';' before identifier 'num'
    1>c:\users\lai\documents\visual studio 2008\projects\1.6.1\1.6.1\exercise.cpp(8) : error C2065: 'num' : undeclared identifier
    1>c:\users\lai\documents\visual studio 2008\projects\1.6.1\1.6.1\exercise.cpp(9) : error C2065: 'cout' : undeclared identifier
    1>c:\users\lai\documents\visual studio 2008\projects\1.6.1\1.6.1\exercise.cpp(9) : error C2059: syntax error : 'bad suffix on number'
    1>c:\users\lai\documents\visual studio 2008\projects\1.6.1\1.6.1\exercise.cpp(9) : error C2146: syntax error : missing ';' before identifier 'num'
    1>c:\users\lai\documents\visual studio 2008\projects\1.6.1\1.6.1\exercise.cpp(9) : error C2065: 'num' : undeclared identifier
    1>c:\users\lai\documents\visual studio 2008\projects\1.6.1\1.6.1\exercise.cpp(9) : error C2065: 'endl' : undeclared identifier
    1>Build log was saved at "file://c:\Users\Lai\Documents\Visual Studio 2008\Projects\1.6.1\1.6.1\Debug\BuildLog.htm"
    1>1.6.1 - 18 error(s), 2 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


    the code i did i got this

    1>------ Build started: Project: 1.6.1, Configuration: Debug Win32 ------
    1>Compiling...
    1>exercise.cpp
    1>c:\users\lai\documents\visual studio 2008\projects\1.6.1\1.6.1\exercise.cpp(11) : error C2062: type 'float' unexpected
    1>c:\users\lai\documents\visual studio 2008\projects\1.6.1\1.6.1\exercise.cpp(13) : error C2059: syntax error : 'bad suffix on number'
    1>c:\users\lai\documents\visual studio 2008\projects\1.6.1\1.6.1\exercise.cpp(13) : error C2146: syntax error : missing ';' before identifier 'num'
    1>c:\users\lai\documents\visual studio 2008\projects\1.6.1\1.6.1\exercise.cpp(13) : error C2065: 'num' : undeclared identifier
    1>c:\users\lai\documents\visual studio 2008\projects\1.6.1\1.6.1\exercise.cpp(13) : error C2563: mismatch in formal parameter list
    1>c:\users\lai\documents\visual studio 2008\projects\1.6.1\1.6.1\exercise.cpp(13) : error C2568: '<<' : unable to resolve function overload
    1> c:\program files\microsoft visual studio 9.0\vc\include\ostream(974): could be 'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> &)'
    1> with
    1> [
    1> _Elem=wchar_t,
    1> _Traits=std::char_traits<wchar_t>
    1> ]
    1> c:\program files\microsoft visual studio 9.0\vc\include\ostream(966): or 'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> &)'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> c:\program files\microsoft visual studio 9.0\vc\include\ostream(940): or 'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> &)'
    1>Build log was saved at "file://c:\Users\Lai\Documents\Visual Studio 2008\Projects\1.6.1\1.6.1\Debug\BuildLog.htm"
    1>1.6.1 - 6 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    i even tried like this knowing that the variable num is undeclaired i got this error

    #include <iostream>
    #include <string>

    using namespace std;

    int main ()
    {
    string str = "Hello world!" ;
    cout << str << endl;
    cout << float x = 5.0f * str << end;
    int 65num = 65;
    cout << " 65num = " << 65num << endl;

    }



    1>------ Build started: Project: new, Configuration: Debug Win32 ------
    1>Compiling...
    1>new.cpp
    1>c:\users\lai\documents\visual studio 2008\projects\new\new\new.cpp(10) : error C2062: type 'float' unexpected
    1>c:\users\lai\documents\visual studio 2008\projects\new\new\new.cpp(11) : error C2059: syntax error : 'bad suffix on number'
    1>c:\users\lai\documents\visual studio 2008\projects\new\new\new.cpp(11) : warning C4091: '' : ignored on left of 'int' when no variable is declared
    1>c:\users\lai\documents\visual studio 2008\projects\new\new\new.cpp(11) : error C2143: syntax error : missing ';' before 'constant'
    1>c:\users\lai\documents\visual studio 2008\projects\new\new\new.cpp(11) : error C2146: syntax error : missing ';' before identifier 'num'
    1>c:\users\lai\documents\visual studio 2008\projects\new\new\new.cpp(11) : error C2065: 'num' : undeclared identifier
    1>c:\users\lai\documents\visual studio 2008\projects\new\new\new.cpp(12) : error C2059: syntax error : 'bad suffix on number'
    1>c:\users\lai\documents\visual studio 2008\projects\new\new\new.cpp(12) : error C2146: syntax error : missing ';' before identifier 'num'
    1>c:\users\lai\documents\visual studio 2008\projects\new\new\new.cpp(12) : error C2065: 'num' : undeclared identifier
    1>c:\users\lai\documents\visual studio 2008\projects\new\new\new.cpp(12) : error C2563: mismatch in formal parameter list
    1>c:\users\lai\documents\visual studio 2008\projects\new\new\new.cpp(12) : error C2568: '<<' : unable to resolve function overload
    1> c:\program files\microsoft visual studio 9.0\vc\include\ostream(974): could be 'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> &)'
    1> with
    1> [
    1> _Elem=wchar_t,
    1> _Traits=std::char_traits<wchar_t>
    1> ]
    1> c:\program files\microsoft visual studio 9.0\vc\include\ostream(966): or 'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> &)'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> c:\program files\microsoft visual studio 9.0\vc\include\ostream(940): or 'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> &)'
    1>Build log was saved at "file://c:\Users\Lai\Documents\Visual Studio 2008\Projects\new\new\Debug\BuildLog.htm"
    1>new - 10 error(s), 1 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    Last edited by Driku; April 7th, 2010 at 08:33 PM.

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: help, whats wrong with this code?

    Here is a simple "Hello World" program:
    Code:
    #include <iostream>
    
    int main()
    {
       std::cout << "Hello World";
    }
    Does this compile? If not, work on this before embarking on something else.

    Secondly:
    Code:
    int 65num = 65;
    You cannot start variable names with digits.

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Jun 2007
    Location
    MA-USA
    Posts
    247

    Re: help, whats wrong with this code?

    Start by correcting first error then recompile and repeat...
    Maybe to clarify, ask for output listing from instructor.
    Does he want to actually multiply 5.0 by each char in string?
    Or just to print "float x = 5.0" or what ???

  9. #9
    Join Date
    Apr 2010
    Posts
    6

    Re: help, whats wrong with this code?

    Quote Originally Posted by Paul McKenzie View Post
    Here is a simple "Hello World" program:
    Code:
    #include <iostream>
    
    int main()
    {
       std::cout << "Hello World";
    }
    Does this compile? If not, work on this before embarking on something else.

    Secondly:
    Code:
    int 65num = 65;
    You cannot start variable names with digits.

    Regards,

    Paul McKenzie


    i didnt tryied yet but i am using the namespace std; isnt suppose to be the same? i know too that i cant start a variable with a number but there are much more error befor that and i wanted to give you all the ppicture of the code

  10. #10
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: help, whats wrong with this code?

    The very first thing you need in order to solve this problem is to get a clarification on what this:
    Code:
    float x = 5.0f * str
    is intended to do. Looking at that, I have no idea what the intended behavior is, therefore I have no idea how to fix it. It's just garbage. You need to know the intent before you even worry about fixing it.

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: help, whats wrong with this code?

    Quote Originally Posted by Driku View Post
    i didnt tryied yet but i am using the namespace std; isnt suppose to be the same? i know too that i cant start a variable with a number but there are much more error befor that and i wanted to give you all the ppicture of the code
    Comment all but the bare minimum of code. Then when it compiles and runs, uncomment the code line by line.

    Start with:

    Code:
    #include <iostream> 
    #include <string>
    
    using namespace std;
    int main ()
    {
     string str ;
     cout << "Hello world!";
    
    /* cin >> str;
     cout << endl;
     cout << float x = 5.0f * str << endl;
     int num65 = 65;
     cout << " 65num = " << 65num << endl; */
    
     return 0;
    }
    Notice I added a return 0; statement at the end.

  12. #12
    Join Date
    Apr 2010
    Posts
    6

    Re: help, whats wrong with this code?

    Quote Originally Posted by Lindley View Post
    The very first thing you need in order to solve this problem is to get a clarification on what this:
    Code:
    float x = 5.0f * str
    is intended to do. Looking at that, I have no idea what the intended behavior is, therefore I have no idea how to fix it. It's just garbage. You need to know the intent before you even worry about fixing it.
    thats my problem too, since its a lecture from a program with pdf i have way to get help but asking help to you guys, i think what he wanted with this was to convert the float to char or something like that but idk, since i cant figure out that part of the code too maybe it is just that, garbage to confuse our minds no?
    Last edited by Driku; April 8th, 2010 at 08:54 AM.

  13. #13
    Join Date
    Apr 2010
    Posts
    6

    Talking Re: help, whats wrong with this code?

    Quote Originally Posted by Arjay View Post
    Comment all but the bare minimum of code. Then when it compiles and runs, uncomment the code line by line.

    Start with:

    Code:
    #include <iostream> 
    #include <string>
    
    using namespace std;
    int main ()
    {
     string str ;
     cout << "Hello world!";
    
    /* cin >> str;
     cout << endl;
     cout << float x = 5.0f * str << endl;
     int num65 = 65;
     cout << " 65num = " << 65num << endl; */
    
     return 0;
    }
    Notice I added a return 0; statement at the end.
    that way will make the rest of the code a coment and i wanted to know the rest of the code how to fix it, since there is no way to solve this without asking to the guy who done this pdf i think ill just keep moving foward since i made all the other codes i gess i passed

  14. #14
    Join Date
    Apr 2010
    Posts
    3

    Re: help, whats wrong with this code?

    you may change the code :
    #include <iostream>
    #include <windows.h>//for system("pause")
    #include <string>
    using namespace std;

    using namespace std;
    int main ()
    {
    string str ;
    cout << "Hello world!"<<endl;
    cin >> str;
    cout <<str<<endl;
    //cout << float x = 5.0f * str << endl;
    int num65 = 65;
    //cout << " 65num = " << 65num << endl;
    cout<<"65num="<<num65<<endl;
    system("pause");//you can use "getchar()" instead
    return 0;
    }

  15. #15
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: help, whats wrong with this code?

    Quote Originally Posted by hefan View Post
    you may change the code :
    This board doesn't provide homework answers. Please edit your post accordingly.

    Looking at your "correction"
    Code:
    //cout << " 65num = " << 65num << endl;
    cout<<"65num="<<num65<<endl;
    I don't see how your post is an improvement. Removing white space hampers readability and actually made it worse IMHO.

Page 1 of 2 12 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
  •  





Click Here to Expand Forum to Full Width

Featured