C++ Error (NEED HELP!)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Xillez
    New Member
    • Jul 2013
    • 93

    C++ Error (NEED HELP!)

    Hi again, X here

    Yet again I have a problem with my learnig files...

    error:

    "Expected primary-expression before "char"" on line 17

    My code for now:

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string>        //NOTE THIS ONE!!!!!!
    
    using namespace std;
    
    string String;
    char UserString[250];
    char inputAnswer[250];
    
    int main()
    {
        string String = "I AM A STRING!!!!!!";
        cout << String << endl;
        cout << "" << endl;
        cout << "Please write a word... " << endl;
        cin.getline(UserString, 250, "\n");
        cout << "Did you type: " << endl;
        cout << UserString << endl;
        cout << "If so write 'Yes' if not write 'No' and press enter... " << endl;
        cin >> inputAnswer;
        if (bool inputAnswer = "yes" || "Yes")
        {
            cout << "Very well then... " << endl;
            return false;
        }
        else
        {
            if (bool inputAnswer = "no" || "No")
            {
                cout << "Sorry! Contact programmer!" << endl;
                return false;
                };
        };
        
        system("PAUSE");
        return EXIT_SUCCESS;
    };
    Some help is greatly appreciated...

    X
  • divideby0
    New Member
    • May 2012
    • 131

    #2
    why do you have semi-colons after some of the closing brackets? get rid of those. cin.getline should be '\n' and not "\n" as it's a single character.

    I'm not really beyond the basics of cpp, but if you plan on using c-style string functions, you may need to include <cstring>.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      ER...

      Code:
      if (bool inputAnswer = "yes" || "Yes")
      inputAnswer is a bool. "yes" and Yes" both exist and have addresses. The addresses are not zero. Therefore inputAnswer is always true.

      Notice that this inputAnswer is not the same one defined earlier in the code.

      What are you trying to do here?

      Comment

      • Xillez
        New Member
        • Jul 2013
        • 93

        #4
        "yes" and "yes" is basicly the input from the person that uses the program...
        Code:
        if (bool inputAnswer = "yes" || "Yes")
            {
                cout << "Very well then... " << endl;
                return false;
            }
            else
            {
                if (bool inputAnswer = "no" || "No")
                {
                    cout << "Sorry! Contact programmer!" << endl;
                    return false;
                    }
            }
        weaknessforcats ...

        The thing is basicly to check if the User typed "Yes", it will say "very well then... " and exit the program, if not it's gone return false and go to the other if statement with the "no" and so on...

        I know. inputAnswer is not gone be defined until the user types "yes" or "no" and that data is gone be pushed in to inputAnswer. So should I move "char inputAnswer;" from a public variable to a local one?

        Comment

        • divideby0
          New Member
          • May 2012
          • 131

          #5
          if you're looking to compare what the user types and gets stored the char version of inputAnswer, then use strcmp or strncmp.

          Code:
          if(strncmp(inputAnswer, "yes", 3) == 0 || strncmp(inputAnswer, "Yes", 3) == 0)
             // user typed yes or Yes
          You've also got "YES", "YeS", "yES" etc. To get away from the cases, convert the string to one case or the other.

          Code:
          for(int i=0; (inputArray[i] = tolower(inputArray[i])) != '\0'; ++i){}
          
          if(strncmp(inputAnswer, "yes", 3) == 0)
             // user wants yes
          An easier way may be just to check if 'y' or 'Y' is entered.

          Code:
          if(tolower(inputAnswer[0]) == 'y')
             // user wants "yes"

          Comment

          • Xillez
            New Member
            • Jul 2013
            • 93

            #6
            Thanks again for the help... I'll be back sometime in the future I persume...

            Thanks again...
            X

            Comment

            Working...