Results 1 to 6 of 6

Thread: char to int

  1. #1

    Thread Starter
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330

    char to int

    Code:
    int convert()
    {
    	char str[10];
    	int len;
    	int ret,i;
    
    cout<<"Enter a binary value";
    cin>> str;
    
    len = strlen(str);
    
    int val;
    
    for(i=len;i>=0;i--){
    	val = atoi(str[i]);//error here
    	//ret = pow(val, i);
    	cout << val<<endl;
    	return 0;
    }
    }
    i get the error : cannot convert parameter 1 from 'char' to 'const char *'

    what do i have to do to correct it? thanks
    Matt

  2. #2
    How about just atoi('x')?

  3. #3
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396

    Re: char to int

    Originally posted by MPrestonf12
    Code:
    int convert()
    {
    	char str[10];
    	int len;
    	int ret,i;
    
    cout<<"Enter a binary value";
    cin>> str;
    
    len = strlen(str);
    
    int val;
    char str2[2];
    
    for(i=len;i>=0;i--){
                   str2[0]=str[i];
                   str2[1]='\0';	
                   val = atoi(str2);//error here
    	//ret = pow(val, i);
    	cout << val<<endl;
    	return 0;
    }
    }
    How about that? the parameter to atoi must be a string which end with null('\0'). cannot be a single char by itself.

    Better to use kedaman's method here.converting char type to int
    I'm a VB6 beginner.

  4. #4
    Lively Member
    Join Date
    Dec 2000
    Location
    Indiana
    Posts
    73
    Originally posted by CornedBee
    Also, I recommend replacing isbin by a macro in C and an inline function in C++.
    Why?

    Wouldnt a macro be more efficient? Less overhead? inline functions still have to get processed....
    if(GetWindowLong(hwnd,GWL_ID)==IDC_MICROSOFT_APPLICATION)
    {
    SetWindowText(hwnd,"I suck.");
    SendMessage(hwnd,WM_START_SUCKING,0,0);
    SendMessage(hwnd,WM_CRASH,0,0);
    }

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    No. if you optimize for speed (not code size), inlines will behave just like macros.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6
    jim mcnamara
    Guest
    CB is right. Think about what a macro does. The compiler expands the macro and puts the code in the position where the macro was already.

    When you inline f() it takes the f() code and moves it to the position where the function is called. Expands all the code onto the line - just like a macro.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width