Thread: problems converting char to int

  1. #1
    The code loser
    Join Date
    Mar 2007
    Posts
    13

    problems converting char to int

    here is my code
    Code:
    #include <iostream>
    #include <stdlib.h>
    
    using namespace std; // using standard namespace
    
    int main ()
    {
    	char date[10], fname[20], lname[20];
    	int d1, d2, m1, m2, y1, y2, y3, y4, year, day, month, ex1t;
    
    	cout <<"enter 0 instead of your first name to exit"<<endl<<endl ;
    	
    	for (int x=1; x>0; x++)
    	{
    		cout <<"enter you first name: " ;
    		cin >>fname ;
    		ex1t=atoi(fname[0]);
    		if (ex1t==0)
    		exit(0);
    		cout <<"enter your last name: " ;
    		cin >>lname ;
    		cout << "enter your birthdate in the format mm/dd/yyyy: " ;
    		cin >>date ;
    		d1=atoi(date[3]);
    		d2=atoi(date[4]); 
    		m1=atoi(date[0]); 
    		m2=atoi(date[1]); 
    		y1=atoi(date[6]);
    		y2=atoi(date[7]); 
    		y3=atoi(date[8]); 
    		y4=atoi(date[9]);
    		year=(y1*1000)+(y2*100)+(y3*10)+(y4);
    		month=(m1*10)+(m2);
    		day=(d1*10)+(d2);
    		
    		if ((month<12||month>1))
    
    		if (month==(1||3||5||7||8||10||12)&&(day<32))
    		cout <<lname <<", "<<fname <<", " <<date <<". "<< endl<<endl;
    
    		if (month==(4||6||9||11)&&(day<31))
    		cout <<lname <<", "<<fname <<", " <<date <<". "<< endl<<endl;
    	}
    	return 0;
    }
    and here are the errors
    Code:
    --------------------Configuration: program4 - Win32 Debug--------------------
    Compiling...
    program4.cpp
    H:\program4.cpp(22) : error C2664: 'atoi' : cannot convert parameter 1 from 'char' to 'const char *'
            Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    H:\program4.cpp(29) : error C2664: 'atoi' : cannot convert parameter 1 from 'char' to 'const char *'
            Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    H:\program4.cpp(30) : error C2664: 'atoi' : cannot convert parameter 1 from 'char' to 'const char *'
            Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    H:\program4.cpp(31) : error C2664: 'atoi' : cannot convert parameter 1 from 'char' to 'const char *'
            Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    H:\program4.cpp(32) : error C2664: 'atoi' : cannot convert parameter 1 from 'char' to 'const char *'
            Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    H:\program4.cpp(33) : error C2664: 'atoi' : cannot convert parameter 1 from 'char' to 'const char *'
            Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    H:\program4.cpp(34) : error C2664: 'atoi' : cannot convert parameter 1 from 'char' to 'const char *'
            Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    H:\program4.cpp(35) : error C2664: 'atoi' : cannot convert parameter 1 from 'char' to 'const char *'
            Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    H:\program4.cpp(36) : error C2664: 'atoi' : cannot convert parameter 1 from 'char' to 'const char *'
            Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    Error executing cl.exe.
    
    program4.exe - 9 error(s), 0 warning(s)
    what is im doing wrong?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    atoi is used to convert a string to an int. If you have a single digit character and want to convert it to an int, simply subtract the zero character:
    Code:
    d1=date[3] - '0';
    >> if (month==(1||3||5||7||8||10||12)&&(day<32))
    That is not the correct use of multiple ||'s in an if statement.

    I'd also suggest using C++ strings instead of character arrays. Among other benefits they won't require you to restrict the names to 20 characters.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    char date[10];
    /* ... */
    d1=atoi(date[3]);
    atoi takes a string, that is a char[], but you pass it a single character. The compiler complains about it. Either use a 2D array, or . . . . Why not just cin directly into the numbers?
    Code:
    cin >> d1;
    cin >> d2;
    //...
    or
    Code:
    cin >> d1 >> d2 >> // ...
    Code:
    if (month==(1||3||5||7||8||10||12)&&(day<32))
    You can't do that. You need to use
    Code:
    if(month == 1 || month == 3 //...)
    or a switch statement
    Code:
    switch(month) {
    case 1:
    case 3:
    // ...
    stuff;
    break;  /* MUST have a break statement
        unless you want to experience the fall-though effect */
    }
    Code:
    if ((month<12||month>1))
    Don't you mean <=12?

    Pray do tell, what is the purpose of this?
    Code:
    for (int x=1; x>0; x++)
    [edit] Having a variable named "ex1t" is going to cause confusion. Call it "quit" or something else. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    The code loser
    Join Date
    Mar 2007
    Posts
    13
    to daved. im gonna try and use casting

    to dwks.
    1. its part of the assignment to use a char array
    2. thanks for the tip on ||
    3. i meant < 13
    4. its an infinite loop. until the user chooses to exit it. (the user is not supposed to know before hand how many entries are to be made)(i always use for loops =habit)

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Common variations of infinite loops are:
    Code:
    for (;;)
    {
    //...
    }
    while(true)
    {
    //...
    }
    Your loop isn't actually infinite since you're using a signed integer
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    The code loser
    Join Date
    Mar 2007
    Posts
    13
    well at least its going to run quite a few times. which is what i want

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by radiantarchon28 View Post
    to daved. im gonna try and use casting

    to dwks.
    1. its part of the assignment to use a char array
    Okay. Then read in the whole line and use stringstreams (see the FAQ) to parse the numbers from it.

    2. thanks for the tip on ||
    3. i meant < 13
    Same thing. <=12 would be easier to read, however, because people know that there are 12 months in a year, but when they see 13 they think, "what?".
    4. its an infinite loop. until the user chooses to exit it. (the user is not supposed to know before hand how many entries are to be made)(i always use for loops =habit)
    If you insist upon using a for loop take JaWiB's suggestion and use
    Code:
    for(;;) {
        // ...
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> to daved. im gonna try and use casting
    I don't think that will work, but maybe you are talking about something different than what I'm thinking of. Can you give an example?

    >> its part of the assignment to use a char array
    A char array represents a string (string meaning a series of 0 or more characters, which is different than the C++ string class). atoi takes a character array that represents a string, but your code attempts to call atoi with a single character in the string.

  9. #9
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    Code:
    cout << "enter your birthdate in the format mm/dd/yyyy: " ;
    cin >>date ;
    d1=atoi(date[3]);
    d2=atoi(date[4]); 
    m1=atoi(date[0]); 
    m2=atoi(date[1]); 
    y1=atoi(date[6]);
    y2=atoi(date[7]); 
    y3=atoi(date[8]); 
    y4=atoi(date[9]);
    Try this, it's a lot easier:
    Code:
    cout << "enter your birthdate in the format mm/dd/yyyy: " ;
    cin >>date ;
    date[2] = '\0';
    date[5] = '\0';
    month = atoi( date );
    day = atoi( &date[3] );
    year = atoi( &date[6] );

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Try this, it's a lot easier
    Easier, maybe, but it's doubtful that's a good idea for an assignment.

  11. #11
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    Why?

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Because using it would be using a trick that is not normally taught in class. Also, given the OP's original code, I think it is unlikely that he or she would understand it very well and using a trick you got from the internet without understanding it kind of defeats the purpose of an assignment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C problem with legacy code
    By andy_baptiste in forum C Programming
    Replies: 4
    Last Post: 05-19-2008, 06:14 AM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  4. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  5. I still don't get it
    By sketchit in forum C Programming
    Replies: 1
    Last Post: 09-27-2001, 11:03 AM