Adblock breaks this site

Help with C++

Discussion in 'Programming General' started by Ryaxxx, Aug 23, 2008.

  1. Ryaxxx

    Ryaxxx Member

    Joined:
    Aug 13, 2008
    Posts:
    29
    Referrals:
    1
    Sythe Gold:
    0
    Help with C++

    I'm new to using strings and i need help. I know something is completely wrong but i'm not sure what to do.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    string string;
    
    int string1 = string1;
    int string2 = string2;
    
    int main ()
    {
        cout << "What's your name?" << endl;
        cin >> string1;
        cout << "Hello " << string1 << endl;
        system("PAUSE");
        cout << "Do you have a favorite color?" << endl;
        cin >> string2;
        cout << "I think the color " << string2 << " is cool" << endl;
        system("PAUSE");
        return 0;
    }
    (This is from Raid500's Basics of C++ Thread with a few changes)
     
  2. Swan

    Swan When They Cry...
    Retired Global Moderator

    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner
    Help with C++

    Added code tags to your post. Please use them in the future.

    I recommend not using Raid's tutorials, they're useless.

    Anyway, you're declaring integers. You're trying to use numbers, rather than strings, in other words. Also, you're dynamically assigning "string1" to "string1", when no member called string1 has been declared. Same with "string2".

    You also don't need to use globally scoped variables. Just variables in main() will do.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main ()
    {
        char string1[80];
        char string2[80];
    
        cout << "What's your name?" << endl;
        cin >> string1;
        cout << "Hello " << string1 << endl;
        cout << "Do you have a favorite color?" << endl;
        cin >> string2;
        cout << "I think the color " << string2 << " is cool" << endl;
        return 0;
    }
    
    I prefer to use the C functions known as printf() and scanf(), however I guess it's a matter of preference.

    strings are character arrays. What Raid's tutorial uses is the string class, which though usable, isn't really preferable.
     
  3. Ryaxxx

    Ryaxxx Member

    Joined:
    Aug 13, 2008
    Posts:
    29
    Referrals:
    1
    Sythe Gold:
    0
    Help with C++

    sorry I forgot the code tags. >.<

    I noticed Raid's Tutorial isn't really a tutorial. Anyway, I changed the script and ran the program, but when i type in my name it would respond Hello0 instead of Hello(name). It also does the same thing with the color. Any suggestions?
     
  4. Swan

    Swan When They Cry...
    Retired Global Moderator

    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner
    Help with C++

    in your original code (I'm assuming you edited yours, mine works perfectly well), you used integers instead of strings.

    In other words, your program is waiting for the input of a number and receiving a string. A string is an array of characters - look at my code.
     
  5. Ryaxxx

    Ryaxxx Member

    Joined:
    Aug 13, 2008
    Posts:
    29
    Referrals:
    1
    Sythe Gold:
    0
    Help with C++

    Oops!

    I accidentally opened up my old string program instead of the new one, so now it works fine.

    Thanks for all your help.
     
  6. Nullware

    Nullware Guru

    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0
    Help with C++

    That may be true, but make sure you actually understand what's going on now or you've gained nothing from it. Feel free to ask questions should anything trouble you.
     
  7. Ryaxxx

    Ryaxxx Member

    Joined:
    Aug 13, 2008
    Posts:
    29
    Referrals:
    1
    Sythe Gold:
    0
    Help with C++

    I understand it. The string is used to call what the person typed and use it.
     
  8. The Hack Masta

    The Hack Masta Guest

    Referrals:
    0
    Help with C++

    Use Some other c++ compiler
     
  9. Swan

    Swan When They Cry...
    Retired Global Moderator

    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner
    Help with C++

    It seems you are either unknowledgable, ignorant, or both.

    It has nothing to do with the damn compiler, first of all.

    Second of all, this has been solved.
     
< [Source] World parsing stuff | Print at my own spot.... >


 
 
Adblock breaks this site