• Log in or sign up for an account here: https://www.sythe.org/login

Help with C++

Ryaxxx

Member
Joined
Aug 13, 2008
Sythe Gold
0
Messages
29
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)
 
Member of the Month Winner
Sythe's 10th Anniversary
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.
 
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?
 
Member of the Month Winner
Sythe's 10th Anniversary
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?
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.
 
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.

Oops!

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

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

Thanks for all your help.
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.
 
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.
I understand it. The string is used to call what the person typed and use it.
 
Member of the Month Winner
Sythe's 10th Anniversary
Use Some other c++ compiler
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.
 
Runescape Gamblingrunescape gambling
Back
Top