0

Can someone help me to debug this problem in C++? I want it to loop if the user want to enter once again, but the error keeps on showing up.

void typeA(){
    int j;
    char dec;
    do{
        cout << "Enter Month: ";
        cin >> month[j];
        cout << "Enter Date: ";
        cin >> date[j];
        cout << "Enter Time in Hour: ";
        cin >> aHours[j];
        cout << "Enter Time in Minutes: ";
        cin >> aMins[j];
        cout << "Enter Time Out Hour: ";
        cin >> aHours[j];
        cout << "Enter Time Out Minutes: ";
        cin >> aMins[j];

        cout << "Enter Again?: [y/n]";
        cin >> dec;

    }while(!dec.compare('y'));
    cout << "Exit";

}
Borgleader
  • 15,826
  • 5
  • 46
  • 62
Raven
  • 5
  • 4
  • Hi Raven! Could you please paste absolutely all of your code, even `#include` statements that might not seem important. (If you don't have any `#include` statements, that would be your problem right there.) – Arthur Tacca Aug 29 '18 at 14:50
  • 3
    `!dec.compare('y')` char has no `operator.` because it is not a union/struct as the error says. I dont know what you were attempting to do with that. most likely dec != 'y' ? – Borgleader Aug 29 '18 at 14:51
  • 1
    Your problem can be reduced to `char dec; dec.compare;`. – melpomene Aug 29 '18 at 14:51
  • 2
    `char` is a basic type. In C++, basic types can't have methods. You should simply compare `dec != 'y'` – Yksisarvinen Aug 29 '18 at 14:51
  • 2
    @ArthurTacca No #include will fix trying to call a non-existant `compare` method on a `char` – Borgleader Aug 29 '18 at 14:51
  • 1
    @Borgleader But it would've helped me paste the code and run it through a compiler and see where the error was without having to pick through it myself. At the moment it will probably complain about unknown symbol `cout`. (Pasting the exact error message including the line number would probably have sufficed too.) Plus it is good practice to let new people know about. – Arthur Tacca Aug 29 '18 at 14:53
  • I'm trying to compare the decision of the user. I think my statement if incorrect can you help me to correct it? – Raven Aug 29 '18 at 14:57
  • 1
    Please **[edit]** your question with an [mcve] or [SSCCE (Short, Self Contained, Correct Example)](http://sscce.org) – NathanOliver Aug 29 '18 at 14:58
  • @Raven As the other people stated, char does not have compare (or any other method). Are you mistaking it for string? Simply write `dec != 'y'` as already stated. != and == are also perfectly fine on string, by the way. – Aziuth Aug 29 '18 at 15:00
  • 2
    @Raven If you think your statement is correct, you should start with [a good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). C++ is not Java or Python and don't try to draw parallels. `char` has no methods. You can't call a method on `char` variable. – Yksisarvinen Aug 29 '18 at 15:01
  • You cannot "debug" compiler errors, you just need to fix them – Slava Aug 29 '18 at 15:04
  • Thank You everyone. You helped me fix my problem :) – Raven Aug 29 '18 at 15:07
  • @Raven `j` is uninitialized and doesn't change between iterations of the loop. `month`, `date`, `aHours`, `aMins`, `aHours` and `aMins` are undefined. Are you using global variables? If so, stop it. – Swordfish Aug 29 '18 at 15:13
  • okay sir. ill take not all your responses. =) – Raven Aug 29 '18 at 15:16

1 Answers1

1

You should use == operator to check if two fundamental types are equal or not, because the fundamental types in C++ can't have methods.

void typeA(){
    int j;
    char dec;
    do{
        cout << "Enter Month: ";
        cin >> month[j];
        cout << "Enter Date: ";
        cin >> date[j];
        cout << "Enter Time in Hour: ";
        cin >> aHours[j];
        cout << "Enter Time in Minutes: ";
        cin >> aMins[j];
        cout << "Enter Time Out Hour: ";
        cin >> aHours[j];
        cout << "Enter Time Out Minutes: ";
        cin >> aMins[j];

        cout << "Enter Again?: [y/n]";
        cin >> dec;

    }while(dec == 'y');
    cout << "Exit";

}
Mohit
  • 1,225
  • 11
  • 28