convert string in to integer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • haniya ali
    New Member
    • Sep 2013
    • 6

    convert string in to integer

    Code:
    struct time
    { int t;
    	int h,m,s;	
    };
    
    int main()
    {
    	time t;
    	int totalsecond;
    	char A[10],B[10];
    	cout<<"Enter the time Format [12:59:59] = ";
    	cin.getline(A,10);
    	int i=0,j=0;
    	while(A[i]!=':')
    	{
    		B[j]=A[i];
    		i++;
    		j++;
    	}
    	t.h=atoi(B);
    	i++;
    	j=0;
    	while(A[i]!=':')
    	{
    		B[j]=A[i];
    		i++;
    		j++;
    	}
    	t.m=atoi(B);
    	i++;
    	j=0;
    	while(A[i]!='\0')
    	{
    		B[j]=A[i];
    		i++;
    		j++;
    	}
    	t.s=atoi(B);
    	
    	totalsecond=(t.h*3600)+(t.m*60)+t.s;
    	cout<<"\n\nTotal Seconds are = "<<totalsecond;
    	
    	cout<<endl;
    	getch();
    	
    }
    it gives error at line no 12. any one resolve it?
    Last edited by weaknessforcats; Sep 26 '13, 06:50 PM. Reason: added code tags
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    What error are you getting?

    Comment

    • haniya ali
      New Member
      • Sep 2013
      • 6

      #3
      i am getting error at line no 8. t undeclared first use this function etc

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        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>?

        Comment

        • haniya ali
          New Member
          • Sep 2013
          • 6

          #5
          yeah, i included it was on line 12

          Comment

          • hosting charges
            New Member
            • Aug 2013
            • 10

            #6
            Try
            Code:
            std::cin.getline

            Comment

            • Sherin
              New Member
              • Jan 2020
              • 77

              #7
              Try This Code

              Code:
              #include <iostream>
              #include <string>
              
              int main() {
              
              	std::string s = "10";
              
              	try
              	{
              		int i = std::stoi(s);
              		std::cout << i << '\n';
              	}
              	catch (std::invalid_argument const &e)
              	{
              		std::cout << "Bad input: std::invalid_argument thrown" << '\n';
              	}
              	catch (std::out_of_range const &e)
              	{
              		std::cout << "Integer overflow: std::out_of_range thrown" << '\n';
              	}
              
              	return 0;
              }

              Comment

              Working...