By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,386 Members | 1,165 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,386 IT Pros & Developers. It's quick & easy.

convert string in to integer

P: 6
Expand|Select|Wrap|Line Numbers
  1. struct time
  2. { int t;
  3.     int h,m,s;    
  4. };
  5.  
  6. int main()
  7. {
  8.     time t;
  9.     int totalsecond;
  10.     char A[10],B[10];
  11.     cout<<"Enter the time Format [12:59:59] = ";
  12.     cin.getline(A,10);
  13.     int i=0,j=0;
  14.     while(A[i]!=':')
  15.     {
  16.         B[j]=A[i];
  17.         i++;
  18.         j++;
  19.     }
  20.     t.h=atoi(B);
  21.     i++;
  22.     j=0;
  23.     while(A[i]!=':')
  24.     {
  25.         B[j]=A[i];
  26.         i++;
  27.         j++;
  28.     }
  29.     t.m=atoi(B);
  30.     i++;
  31.     j=0;
  32.     while(A[i]!='\0')
  33.     {
  34.         B[j]=A[i];
  35.         i++;
  36.         j++;
  37.     }
  38.     t.s=atoi(B);
  39.  
  40.     totalsecond=(t.h*3600)+(t.m*60)+t.s;
  41.     cout<<"\n\nTotal Seconds are = "<<totalsecond;
  42.  
  43.     cout<<endl;
  44.     getch();
  45.  
  46. }
  47.  
it gives error at line no 12. any one resolve it?
Sep 26 '13 #1
Share this Question
Share on Google+
6 Replies

weaknessforcats
Expert Mod 5K+
P: 9,197
What error are you getting?
Sep 26 '13 #2

P: 6
i am getting error at line no 8. t undeclared first use this function etc
Sep 26 '13 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
Before you said it was line 12. Do you mean line 8 on your first post or line 12?

If it turns out to be line 12 have you included <iostream>?
Sep 26 '13 #4

P: 6
yeah, i included it was on line 12
Sep 27 '13 #5

P: 10
Try
Expand|Select|Wrap|Line Numbers
  1. std::cin.getline
  2.  
Sep 27 '13 #6

P: 55
Try This Code

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3.  
  4. int main() {
  5.  
  6.     std::string s = "10";
  7.  
  8.     try
  9.     {
  10.         int i = std::stoi(s);
  11.         std::cout << i << '\n';
  12.     }
  13.     catch (std::invalid_argument const &e)
  14.     {
  15.         std::cout << "Bad input: std::invalid_argument thrown" << '\n';
  16.     }
  17.     catch (std::out_of_range const &e)
  18.     {
  19.         std::cout << "Integer overflow: std::out_of_range thrown" << '\n';
  20.     }
  21.  
  22.     return 0;
  23. }
3 Weeks Ago #7

Post your reply

Sign in to post your reply or Sign up for a free account.