Whats wrong with this?

This is a discussion on Whats wrong with this? within the C++ Programming forums, part of the General Programming Boards category; } while (cont = "y"); for this line I get: invalid conversion from 'const char*' to 'char'...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    20

    Whats wrong with this?

    } while (cont = "y");

    for this line I get:

    invalid conversion from 'const char*' to 'char'

  2. #2
    Registered User
    Join Date
    Dec 2007
    Location
    Denmark
    Posts
    14
    } while (cont == "y");

    (:

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    20

    ...

    duh...

    Oldest mistake in the book.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Location
    Denmark
    Posts
    14
    Yeah, done that a million of times myself :3

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    20

    ...

    Now it thinks "y" is a pointer (I think).

  6. #6
    Registered User
    Join Date
    Dec 2007
    Location
    Denmark
    Posts
    14
    What error does it cast? And a bit more of the code would be good as well..

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    20
    Ok, I fixed that but I have another problem.
    I have this code (you can skip down to the bold):

    #include <iostream>

    using namespace std;

    int bday;
    int bmonth;
    int byear;
    int cday;
    int cmonth;
    int cyear;
    char cont;



    int main()
    {

    do{
    system("cls");
    cout<<"What year were you born in? (example: 1994)" << endl;
    cin>> byear;
    cin.ignore();
    system("cls");
    cout<<"What month of the year were you born in? Please represent numerically (january: 1 , february: 2 , etc.)" << endl;
    cin>> bmonth;
    cin.ignore();
    system("cls");

    cout<<"What day of the month were you born? (example: 10)" << endl;
    cin>> bday;
    cin.ignore();
    system("cls");
    cout<<"What year is it currently? (example: 1994)" << endl;
    cin>> cyear;
    cin.ignore();
    system("cls");
    cout<<"What month is it currently? Please represent numerically (january: 1 , february: 2 , etc.)" << endl;
    cin>> cmonth;
    cin.ignore();
    system("cls");
    cout<<"What day of the month is it currently? (example: 10) \n";
    cin>> cday;
    cin.ignore();
    system("cls");

    cout<<"You have been sleeping for: "<< (cyear - byear)/3 <<" years, " << (cmonth - bmonth)/3 <<" months, and "<< (cday - bday)/3 <<" days.\n(Based on 8 hours of sleep a day, days rounded)" << endl;

    cout<< "1. Restart" <<endl;
    cin>> cont;
    cin.ignore();

    } while (cont == 1);

    }

    --------------------------------

    Yet, upon entering 1 and hitting return, the program quits. Help?

  8. #8
    Registered User
    Join Date
    Dec 2007
    Location
    Denmark
    Posts
    14
    you could always write it like:

    Code:
    } while (cont);
    As this will make the while fail if cont == 0 (and so you should write "(0) to exit. (1) to restart" or something similar)

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    20

    ...

    thank you, that is helpful, but what was wrong in the 1st place?

  10. #10
    Registered User
    Join Date
    Dec 2007
    Location
    Denmark
    Posts
    14
    You could also have changed cont from char to int if you wanted to compare it with 1.

    As for the error with pointers and char I must admit that I've no idea.
    I guess someone more experienced could explain it.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    cont is a char variable. "1" is a string, which is implemented as a const char* (also known as a pointer to a character constant). You can't compare a char to a char pointer.

    When you switched to a 1 instead of a "1", you were then comparing a char to an int. The character '1' is stored as a character code, and that character code is compared against the character code 1. So if the user types 1 it will be converted to a character code (probably 49) which does not equal 1.

    One solution is to use '1' to indicate the character '1' instead of the number 1. Then (cont == '1') will be true if the user types 1.

    Another solution is to switch cont to an int variable, which will allow you to compare it to 1 correctly.

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,556
    Use code tags in the future too. It makes the code look properly indented and kills smilies in the code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 09:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21