Thread: I'm having a bit of a problem

  1. #1
    Registered User
    Join Date
    Apr 2018
    Posts
    4

    I'm having a bit of a problem

    Hello guys, I have recently started using / trying to use c++ and got into making the simplest game ever. Here is the code for it:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    char name[6];
    
    
    main()
    {
        cout << "Ryan: Welcome little guy, what is your name?" << endl;
        cin.get(name[6]);
        cout << "Ryan: So, your name is:" << name << " Cool. I heard you wanna fight and I also heard this is going to be your very first match. Is that right?\n1.Yes\n2.No" << endl;
        if(name[6] == 1)
        cout << "Ryan: Ok, that's cool. We will find an opponent of your age, height and weight, and then we will call you back.\n\n\nTwo days after.." << endl;
        else(name[6] != 1)
            
    
    
    
    }


    The thing is, that after the 2nd cout of the code, the program just stops and returns the value to 1 / 0. And I do not know why, I thought you guys might be able to give me a hint or even the solution for it.
    And also, if you could tell me, how do I stop the program if the "else" from "if" is false?
    so let's say:
    Code:
    if (name[6] == 1)
       //code....
    else (name[6] != 2)
      //how do I close the program if else is not true?
    Thanks in advance guys and sorry for being so noob, but as I said I'm at the beggining.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    I don't get what you're trying to do with those if statements.

    Since you're new to this, I suggest using the much easier "string" class instead of char arrays. See here for details:
    std::string - C++ standard library - Cprogramming.com

    main() is a function like any other, and it should return an int, like this:
    Code:
    int main()
    {
        // Your code goes here
    
        return 0;
    }
    Arrays start counting from zero, so a six-character array would be from "name[0]" to "name[5]".

    If you're trying to get an entire string and not just a single character, use just the name of the array, without any index.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Apr 2018
    Posts
    4
    Yeah well, you didn't answer my questions.. You only told me that I should not use chars and if's considering I only asked why the program stops after the 2nd cout comes in..

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    What do you think line #14 does?
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    1,649
    To use C-style strings you should include <cstring> not <string> (which is for C++ strings). And you need to use strcmp to compare them. It returns 0 if they are equal.
    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
     
    int main() {
        char name[100];
        cout << "Name: \n";
        cin.getline(name, sizeof name);
        cout << "So, your name is: " << name << '\n';
        cout << "Is that right?\n1.Yes\n2.No\n";
        cin.getline(name, sizeof name);
        if (strcmp(name, "Yes") == 0)
            cout << "Ok, that's cool.\n";
        else
            cout << "Yikes.\n";
        return 0;
    }
    To use C++ strings:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
     
    int main() {
        string line;
    
        cout << "Name: \n";
        getline(cin, line);
    
        cout << "So, your name is: " << line << '\n';
    
        cout << "Is that right?\n1.Yes\n2.No\n";
        getline(cin, line);
    
        if (line == "Yes")
            cout << "Ok, that's cool.\n";
        else
            cout << "Yikes.\n";
    
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  6. #6
    Registered User
    Join Date
    Apr 2018
    Posts
    4
    This does not really work, though you made the program not to close after the 2nd cout comes in but when the user input at "if" is Yes or No, the output of the code is "Yikes". I'll try figuring it out though.

  7. #7
    Registered User
    Join Date
    Dec 2017
    Posts
    1,649
    You have to enter Yes exactly the way it is in the comparisons. So the Y needs to be capitalized.

    If you want a case-insensitive comparison, the easiest method is to convert the input string to all upper (or lower) case.
    toupper returns a character in uppercase (and returns non-alpha chars unchanged).
    Code:
    // C-style strings
    
    #include <iostream>
    #include <cstring>
    #include <cctype>
    using namespace std;
      
    int main() {
        char name[100];
        cout << "Name: ";
        cin.getline(name, sizeof name);
        cout << "So, your name is: " << name << '\n';
    
        char line[100];
        cout << "Is that right?\n1.Yes\n2.No\n";
        cin.getline(line, sizeof line);
        int sz = strlen(line);
        for (int i = 0; i < sz; i++) line[i] = toupper(line[i]);
    
        if (strcmp(line, "YES") == 0)
            cout << "Ok, that's cool.\n";
        else
            cout << "Yikes.\n";
    
        return 0;
    }
    
    
    
    // C++ strings
    
    #include <iostream>
    #include <string>
    #include <cctype>
    using namespace std;
      
    int main() {
        string name;
        cout << "Name: ";
        getline(cin, name);
        cout << "So, your name is: " << name << '\n';
    
        string line;
        cout << "Is that right?\n1.Yes\n2.No\n";
        getline(cin, line);
        for (auto& c: line) c = toupper(c);
    
        if (line == "YES")
            cout << "Ok, that's cool.\n";
        else
            cout << "Yikes.\n";
     
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  8. #8
    Registered User
    Join Date
    Apr 2018
    Posts
    4
    Same thing happens, if I delete the for (auto& c: line) c = toupper(c); . Tho, if I don't delete it, the program works fine until Yes / No comes in, and then it stops responding and it returns to a non existing numbers such as -284815811384 or something.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-09-2014, 06:46 PM
  2. Problem passing argument into function, basic problem
    By tsdad in forum C++ Programming
    Replies: 7
    Last Post: 05-22-2013, 12:09 PM
  3. Replies: 2
    Last Post: 01-06-2013, 07:49 AM
  4. Replies: 1
    Last Post: 12-07-2012, 10:00 AM
  5. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM

Tags for this Thread