-2

I am writing a code that accepts 4 values in a single line (the values are x,y coordinates of a vertex in a right angled triangle and x, y coordinates of another vertex in the same triangle) The code then calculates deltaX and deltaY, the length of the hypotenuse and the angle between the second point and the first point using atan2() function.

Then the code outputs/ displays the hypotenuse in the following way: length of hypotenuse(float)+single space+ angle between the points

The code is supposed run many times in the same console: the way it should run

but after i run the code once and press enter to run it once again it throws an error saying that abort() has been called, pls help! the way it is running and the error

The code is:

// Copyright A.T. Chamillard. All Rights Reserved.

#define _USE_MATH_DEFINES

#include <iostream>
#include <string>
#include <cmath>

// IMPORTANT: Only add code in the section
// indicated below. The code I've provided
// makes your solution work with the 
// automated grader on Coursera

// x and y coordinates for points
float Point1X;
float Point1Y;
float Point2X;
float Point2Y;

void GetInputValuesFromString(std::string Input);

/**
 * Programming Assignment 2
 * @return exit status
*/
int main()
{
    // loop while there's more input
    std::string Input;
    std::getline(std::cin, Input);
    while (Input[0] != 'q')
    {
        // extract point coordinates from string
        GetInputValuesFromString(Input);

        // Add your code between this comment
        // and the comment below. You can of
        // course add more space between the
        // comments as needed
`
        float deltaX{ Point2X - Point1X };
        float deltaY{ Point2Y - Point1Y };
        float hypo{ sqrtf(powf(deltaX, 2)+powf(deltaY, 2) )};
        float a{ atan2((Point2Y-Point1Y),(Point2X-Point1X)) };
        a = a * 180 / M_PI;
        std::cout << hypo << " " << a;
`

        // Don't add or modify any code below
        // this comment
        std::getline(std::cin, Input);
    }
}

/**
 * Extracts point coordinates from the given input string
 * @param Input input string
*/
void GetInputValuesFromString(std::string Input)
{
    // extract point 1 x
    int SpaceIndex = Input.find(' ');
    Point1X = std::stof(Input.substr(0, SpaceIndex));

    // move along string and extract point 1 y
    Input = Input.substr(SpaceIndex + 1);
    SpaceIndex = Input.find(' ');
    Point1Y = std::stof(Input.substr(0, SpaceIndex));

    // move along string and extract point 2 x
    Input = Input.substr(SpaceIndex + 1);
    SpaceIndex = Input.find(' ');
    Point2X = std::stof(Input.substr(0, SpaceIndex));

    // point 2 y is the rest of the string
    Input = Input.substr(SpaceIndex + 1);
    Point2Y = std::stof(Input);
}

I tried googling it but i did not get what i was looking for

Chethan R
  • 1
  • 1
  • Please post the desired output and the actual output, as well as the error message, into the question as text. I suggest that you read this: [Why not upload images of code/data/errors when asking a question?](https://meta.stackoverflow.com/q/285551/12149471) However, it may be useful to keep your links to the images inside the question, as an additional reference. – Andreas Wenzel Apr 13 '23 at 05:43
  • An exception was thrown that you did not catch. Now is a great time to learn to use a debugger. Just getting it to crash while debugging will probably be enough to point you to your error. – sweenish Apr 13 '23 at 05:51
  • My guess is that `abort` is being called due to an unhandled exception. Have you tried running your code line-by-line in a debugger while monitoring the control flow and the values of all variables, in order to determine in which line your program stops behaving as intended and whether an exception is being thrown? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) – Andreas Wenzel Apr 13 '23 at 05:51

2 Answers2

1

I ran your code without problems, so this is something of a guess.

The only issue with your code is this

std::cout << hypo << " " << a;

which should really be

std::cout << hypo << " " << a << "\n";

That is you should output a newline after you output the two numbers.

Now my guess is that after the first set of results, and because of the missing output of a newline in your code, you pressed the enter key to move the cursor on to the next line, before you typed the second set of numbers. This is enough to crash your program. The crash happens because pressing the enter key with no input causes getline to read a blank line. Then GetInputValuesFromString will try and read four numbers from the blank line and because that function hasn't been designed to handle that situation it crashes.

Another way of saying this is that the real problem is not with your code, it's with the not terribly well written function GetInputValuesFromString. Any function that is dealing with user input should be validating that input. But you didn't write that function, so you can't be expected to fix it.

The easy solution is not to type the enter key before you input the second set of numbers (and fix the code so that it outputs that newline).

As I said just a guess, but I'm hopeful I've guessed correctly. It's a good lesson in that computer programs do exactly what you tell them to do, no matter how stupid or unexpected that is.

Just re-read what you wrote above 'press enter to run it once again'. That seems to be exactly the problem.

john
  • 85,011
  • 4
  • 57
  • 81
0

You must check the value of SpaceIndex, see std::string::find

Return value

Position of the first character of the found substring or npos if no such substring is found.

int SpaceIndex = Input.find(' ');

Using SpaceIndex (std::string::npos) in the following call to std::string::substr will give an exception or an empty string.

If substr yields an empty string, the call to std::stof will fail with an exception, because no conversion is possible

Exceptions

std::invalid_argument if no conversion could be performed

std::out_of_range if the converted value would fall out of the range of the result type or if the underlying function (strtof, strtod or strtold) sets errno to ERANGE.

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198