please read.....

This is a discussion on please read..... within the C++ Programming forums, part of the General Programming Boards category; Hi, what does this error mean???How can i solve it?? please help a newbie!!!!! thanks. constraint for this code: no ...

  1. #1
    Unregistered
    Guest

    please read.....

    Hi,
    what does this error mean???How can i solve it??
    please help a newbie!!!!!

    thanks.

    constraint for this code: no character arrays allowed;string ONLY.

    Code:
    #include<iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    
    
    #include<string>
    using std::string;
    
    int main()
    {
      
     string time;
    
     int chk=0;
    int x =0;
     cout<<"enter time"<<endl;
    	 cin>>time;
    
    chk=time.find_first_of("1234567890");
    
    if (chk!=-1)
    {
    	cout<<"error!please reenter"<<endl;
    }
    else if (chk==-1)
    {
    	cout<<"well done"<<endl;
    	int x= atoi(time);// error on this line//
    	cout<<x<<endl;
    
    }
    return 0;
    }
    this is the error:
    error C2664: 'atoi' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'
    No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
    Error executing cl.exe.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    212
    Code:
    #include <iostream>
    using namespace std;
    would be easier for a start.

    the atoi(); function is in the standard library. So you have to include the <cstdlib> header.

    kwigibo

  3. #3
    Unregistered
    Guest

    Unhappy thankx ..but

    Hi,

    thanks for pointing out the header library,but still its displaying the same error message.
    can anyone help me out?????
    thankss..in advance.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    101
    Code:
    int x= atoi(time);// error on this line//
    Change this to:
    Code:
    int x= atoi(time.c_str());
    The problem is that atoi expects a const char*, not a std::string.
    - lmov

  5. #5
    Unregistered
    Guest

    Thumbs up thanks a bunch!!

    Hey,
    Thanks Imov,its working nicely now!!Also my sincere thanks to kwigibo,for pointing a silly mistake.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read Causes Freezing?
    By Masna in forum C Programming
    Replies: 5
    Last Post: 07-18-2008, 04:31 PM
  2. Read() problems
    By yay4rei in forum C Programming
    Replies: 2
    Last Post: 07-21-2005, 10:47 AM
  3. How can I know the actual bytes read in a file read
    By pliang in forum C++ Programming
    Replies: 1
    Last Post: 06-08-2005, 04:23 PM
  4. What Would You Use To Read User Input?
    By djwicks in forum C Programming
    Replies: 11
    Last Post: 04-05-2005, 03:32 PM
  5. Read Array pro!!Plz help!!
    By Supra in forum C Programming
    Replies: 2
    Last Post: 03-04-2002, 02:49 PM

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