I writing a program to read several data lines in from a file then using functions to process the data. One of the function is giving me an error that i can't figure out.

The following code works fine with "1/2". However there is a case when the '/' sign is in the 3 position "1 1/2". So i thought i'd just add another if statement.

string temp;
	int a, b;
	string ch4 = "/";
	if (code4.substr(1,1) == ch4)
	{
		temp = code4.substr(0,1);
		a = atoi(temp.c_str());
		temp = code4.substr(2,1);
		b = atoi(temp.c_str());
		double k = double(a)/b;
		cout << setw(15) << left << "Visibility:" << k << " statute miles" << endl;
	}
	
	else
	{
		temp = code4.substr(0,2);
		cout << setw(15) << left << "Visibility:" << temp << " statute miles" << endl;
	}

This code doesn't work with added if statement.

string temp;
	int a, b;
	string ch4 = "/";
	if (code4.substr(1,1) == ch4)
	{
		temp = code4.substr(0,1);
		a = atoi(temp.c_str());
		temp = code4.substr(2,1);
		b = atoi(temp.c_str());
		double k = double(a)/b;
		cout << setw(15) << left << "Visibility:" << k << " statute miles" << endl;
	}
	if (code4.substr(3,1) == ch4)
		{
			temp = code4.substr(0,1);
			a = atoi(temp.c_str());
			temp = code4.substr(2,1);
			b = atoi(temp.c_str());
			temp = code4.substr(4,1);
			int c = atoi(temp.c_str());
			double k = a + double(b)/c;
			cout << setw(15) << left << "Visibility:" << k << " statute miles" << endl;
		}
	
	else
	{
		temp = code4.substr(0,2);
		cout << setw(15) << left << "Visibility:" << temp << " statute miles" << endl;
	}

I get "application has requested the Runtime to terminate it in an unusual way."

Where is my error and how can i fix it?
TIN
Soccerace

Recommended Answers

All 4 Replies

I'm not sure that this info is enough to reproduce and analyze your program error(s). It looks like a very unstable, error-prone code (what happens if code4 string is too short? Right, you catch an exception or get undefined program behaviour).
Some tips:
1. Didn't you know string subscript operator?

if (code4[1] == '/') ...
...
b = code4[2] - '0';
... and so on - you get short and clear code ...

2. Where is else?

if (code4[1] == '/') {
   ...
} else if (code4[3] == '/') { // <= else !!!
   ...
} else {
   ...
}

Okay i made the following changes an i still get the same error. This is the whole function. code4 is a string passed down from main. There is only 3 possible cases it can be. Those are "1/2SM" (will only ever be 1/4 or 3/4 ect...) or "1 1/2SM" (only ever a single digit number followed by space and single digit fractions) or "20SM" (can be 1SM,2SM,3SM....19SM,20SM).

The program compiles and runs the first data line which happens to be "10SM". It stops on the second data line at this function giving me that error. That data line is "1 1/2SM"

int vis(string code4)
{
	string temp;
	double a, b;
	if (code4[1] == '/')
	{
		a = code4[0] - '0';
		b = code4[2] - '0';
		double k = a/b;
		cout << setw(15) << left << "Visibility:" << k << " statute miles" << endl;
	}
	else if (code4[3] == '/')
	{
		double c = code4[0] - '0';
		a = code4[2] - '0';
		b = code4[4] - '0';
		double k = c + a/b;
		cout << setw(15) << left << "Visibility:" << k << " statute miles" << endl;
	}

	else
	{
		temp = code4.substr(0,2);
		cout << setw(15) << left << "Visibility:" << temp << " statute miles" << endl;
	}
	return 0;
}

This program ran correctly for me. Do you get an error?

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;



int vis(string code4)
{
	string temp;
	double a, b;
	if (code4[1] == '/')
	{
		a = code4[0] - '0';
		b = code4[2] - '0';
		double k = a/b;
		cout << setw(15) << left << "Visibility:" << k << " statute miles" << endl;
	}
	else if (code4[3] == '/')
	{
		double c = code4[0] - '0';
		a = code4[2] - '0';
		b = code4[4] - '0';
		double k = c + a/b;
		cout << setw(15) << left << "Visibility:" << k << " statute miles" << endl;
	}

	else
	{
		temp = code4.substr(0,2);
		cout << setw(15) << left << "Visibility:" << temp << " statute miles" << endl;
	}
	return 0;
}


int main ()
{
    string code4 = "1 1/2SM";
    vis (code4);
    cin.get ();
    return 0;
}

wow i feel stupid now. My error was in main(). My get line (stopping at ' ') was only passing "1" to the function instead of "1 1/2SM" hence the error because i was asking for data that wasn't there. To fix it i just added code4 and code5 when code5[1] == '/'. Thanks for the help. My code looks a lot cleaner now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.