Program crashing

This is a discussion on Program crashing within the C++ Programming forums, part of the General Programming Boards category; calc.h Code: #include <iostream> #include <cmath> #include <string> using namespace std; inline void operations() { cout<<"x,/,+,-,^,s(press enter then enter the ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    151

    Program crashing

    calc.h
    Code:
    #include <iostream>
    #include <cmath>
    #include <string>
    
    using namespace std;
    
    inline void operations()
    {
    	cout<<"x,/,+,-,^,s(press enter then enter the number)\n";
    }
    inline float math()
    {
    	string line;
    	float number; 
    	float answer;
       cout<<"enter what you want to do (m for math, o for math operations, s for square root, f for farenheit to celsius, c for celsius to farenheit)\n";
       getline(cin,line);
       if(line.at(0) == 'o'){
    	   operations();
    	   answer = 0;
    	   return answer;
       }
       if(line.at(0) == 's'){
    	   cout<<"enter a number: ";
    	   cin>>number;
    	   answer = sqrt(number); 
    	   return answer;
       }
       if(line.at(0) == 'm'){
    		float x;
            float y;
            char a;
    		float answer;
    		cout<<"enter a problem\n";
    	   cin>>x>>a>>y;
        switch (a){
    	case 'x':
    		answer = x*y;
    		return answer;
    		break;
    	case '/':
    		answer = x/y;
    		return answer;
    		break;
    	case '+':
    		answer = x+y;
    		return answer;
    		break;
    	case '-':
    		answer = x-y;
    		return answer;
    		break;
    	case '^':
    		answer = pow (x,y);
    		return answer;
    		break;
    }
       }
       if(line.at(0) == 'c'){
    	   float degrees;
    
    	   cout<<"enter degrees celsius\n";
    	   cin>> degrees;
    	   answer = 1.8*degrees+32;
    	   return answer;
    } 
       if(line.at(0) == 'f'){
    	   float degrees;
    
    	   cout<<"enter degrees farenheit\n";
    	   cin>> degrees;
    
    	   answer = (degrees-32)*1.8;
    	   return answer;
       }
      int main();
    }
    main.cpp
    Code:
    #include <iostream>
    #include "calc.h"
    
    using namespace std;
    
    int main()
    { 
    	float answer = math();
    	char yn;
    
    	cout<< answer <<"\n";
        cout<<"do you want to calculate something else?<y/n>\n";
    	cin>>yn;
    	if(yn == 'y'){
    		math();
    	}
    	if(yn == 'n'){
    		return 1;
    	}
    }
    When i press y to loop the program the program crashed, any suggestions?
    Thank you.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    When you use cin >> to get the response from the user for "do you want to calculate something else?<y/n>" the user types in y and hits <enter>. That puts two characters in the input buffer, 'y' and a newline, '\n'. The cin >> yn reads in the 'y', but leaves the newline.

    Later, when you call math(), it calls getline. The getline function stops at the first newline character it sees, and since there is still a newline character in the input buffer, it stops immediately and the line variable is empty. You don't check the line variable to see if it is empty, you just use the first character. Since there is no first character, the at(0) call fails and throws an exception which ends your program.

    The solution to the leftover newline is to cal l cin.ignore() after all your calls to cin >>. In this program you probably only need to do it after cin>>yn;, but it doesn't hurt to do it after all calls to cin>> just to be safe.

    You could also put in more error checking. For example, check the line variable after calling getline to make sure it isn't empty before accessing the first character.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. triggering another program to run using serial port
    By infineonintern in forum C++ Programming
    Replies: 3
    Last Post: 07-22-2009, 05:16 AM
  2. need help program crashing
    By tunerfreak in forum C++ Programming
    Replies: 14
    Last Post: 05-22-2006, 11:29 AM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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