Page 1 of 3 123 LastLast
Results 1 to 10 of 26
  1. #1
    Join Date
    Mar 2007
    Location
    Kent UK
    Posts
    30

    Help simple program

    Hi all I was bored so was messing about with c++ got this bit code

    #include <iostream>
    using namespace std;
    int main(){
    int name = 'sian';
    cout << "Please Enter your name!\n";
    cin >> name;

    if (name = 'sian')

    cout <<" I think " << name <<" is amazing.\n";

    cin.get();
    return 0;
    }

    But instead of printing the value name as sian it is printing the value of name as 1985634763 what I think is the memory address of it why would this be?

  2. #2
    Join Date
    Jun 2003
    Location
    Francistan
    Posts
    147
    Hi there.

    You have a few mistakes in your code.

    First of all, if you want the user to enter a name. then expect a string, not an int.
    Code:
    int name = 'sian';
    to be replaced by
    Code:
    char *name; // no need to initialize it as it will get the user input
    Also note that string values are between double quotes ("value") and not single quotes ('wrong').

    Comparison operator is ==, not =.
    = assigns a value to a variable.
    I suggest you use strcmp to compare strings.

    Look at the code below which will tell you that M4cFly rules, anyone else stinks.

    Code:
    #include <iostream>
    #include <string.h>
    using namespace std;
    
    int main () {
    	char *name;
    	cout << "Please Enter your name!\n";
    	cin >> name;
    
    	if (strcmp(name, "M4cFly") == 0)
    		cout << "I think " << name <<" is amazing.\n";
    	else
    		cout << "I think " << name <<" stinks.\n";
    	system("PAUSE");
    	return 0;
    }
    I'm dangerous Jerry, I've got the Kavorka!

    Portfolio

  3. #3
    Join Date
    Mar 2007
    Location
    Kent UK
    Posts
    30
    I thoguht char only hold a single character ?

  4. #4
    Join Date
    Jun 2003
    Location
    Francistan
    Posts
    147
    char does, char * doesn't.

    Here's the alternate code with char name:
    Code:
    #include <iostream>
    #include <string.h>
    using namespace std;
    
    int main () {
    	char name;
    	cout << "Please Enter your name!\n";
    	cin >> name;
    
    	if (name == 'M')
    		cout << "I think " << name <<" is amazing.\n";
    	else
    		cout << "I think " << name <<" stinks.\n";
    	system("PAUSE");
    	return 0;
    }
    In this case only the first character you input will be assigned to name.
    I'm dangerous Jerry, I've got the Kavorka!

    Portfolio

  5. #5
    Join Date
    Mar 2006
    Posts
    372
    Char * is a pointer, millwall. It can practically hold an array of chars, thus a string.

    Another way to do it would be to declare char name[20] , but that is less efficient, since you're allocating exactly 20 characters to the name. If you write more, you can get into some trouble, if you write less you have memory allocated for nothing.

    So pointers are the way to go here.

  6. #6
    Join Date
    Mar 2007
    Location
    Kent UK
    Posts
    30
    Ok but by using chat it only gonna return a value of 1 character. I want it to return the name that they enter so would I be better off using a string for name?

    second when you using an array

    example name[5]

    Does this mean it can only hold 10 characters? So if my name was James that would be fine. If it was superman it would not work as that is more than 5?

  7. #7
    Join Date
    Mar 2006
    Posts
    372
    You wouldn't get any compiler errors if you used char name[5] and assign 'Superman', but it is a very bad programming practice. That's because you have space allocated for 5 characters, if you use 6 or more, you are practically stepping over the limit, on other memory that could be holding something else.

    Again, if you use char *name , assign name = 'Superman' and then cout << name , it will print fine. Read McFly's example again, I think that was pretty suggestive.

  8. #8
    Join Date
    Mar 2007
    Location
    Kent UK
    Posts
    30
    cool thanks all for your help

  9. #9
    Join Date
    Feb 2005
    Location
    Bellevue, WA
    Posts
    3,251
    Quote Originally Posted by M4cFly
    Code:
    char *name; // no need to initialize it as it will get the user input
    This is NOT true. You MUST allocate the memory in which you are reading the variable into (called a buffer). The way you have it, the characters read in will be written to that location in memory, but because you haven't declared this memory as yours, you will be overwriting memory that is not yours.

    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." ~Rich Cook

  10. #10
    Join Date
    Jun 2003
    Location
    Francistan
    Posts
    147
    Quote Originally Posted by owensd
    This is NOT true. You MUST allocate the memory in which you are reading the variable into (called a buffer). The way you have it, the characters read in will be written to that location in memory, but because you haven't declared this memory as yours, you will be overwriting memory that is not yours.
    I don't think I am overwritting memory that's not mine here! What's the point of declaring then?
    What if I initialize a variable to "lol" and the user input is "hello world". Isn't the variable now using more space than after initialization?

    When you declare the variable and initialize it right after, you are overwritting something anyway.

    What will be overwritten (garbage) is not my problem, the system decided I could use that specific memory space.
    I'm dangerous Jerry, I've got the Kavorka!

    Portfolio

Page 1 of 3 123 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
  •