Help with C++

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

Help with C++
  1. Unread #1 - Aug 23, 2008 at 2:58 AM
  2. Ryaxxx
    Joined:
    Aug 13, 2008
    Posts:
    29
    Referrals:
    1
    Sythe Gold:
    0

    Ryaxxx Member

    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)
     
  3. Unread #2 - Aug 23, 2008 at 4:12 AM
  4. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    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.
     
  5. Unread #3 - Aug 27, 2008 at 9:45 PM
  6. Ryaxxx
    Joined:
    Aug 13, 2008
    Posts:
    29
    Referrals:
    1
    Sythe Gold:
    0

    Ryaxxx Member

    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?
     
  7. Unread #4 - Aug 28, 2008 at 2:03 AM
  8. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    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.
     
  9. Unread #5 - Aug 28, 2008 at 4:40 PM
  10. Ryaxxx
    Joined:
    Aug 13, 2008
    Posts:
    29
    Referrals:
    1
    Sythe Gold:
    0

    Ryaxxx Member

    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.
     
  11. Unread #6 - Aug 28, 2008 at 6:27 PM
  12. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    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.
     
  13. Unread #7 - Sep 3, 2008 at 8:56 PM
  14. Ryaxxx
    Joined:
    Aug 13, 2008
    Posts:
    29
    Referrals:
    1
    Sythe Gold:
    0

    Ryaxxx Member

    Help with C++

    I understand it. The string is used to call what the person typed and use it.
     
  15. Unread #8 - Sep 4, 2008 at 2:39 AM
  16. The Hack Masta
    Referrals:
    0

    The Hack Masta Guest

    Help with C++

    Use Some other c++ compiler
     
  17. Unread #9 - Sep 4, 2008 at 6:16 AM
  18. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    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.... >

Users viewing this thread
1 guest


 
 
Adblock breaks this site