inf output computing line slopes - c++

I am very new at C++.
I wrote this code below which is supposed to tell me if 2 lines have an intersection point, so I figured two lines with equal "M" in the y=Mx+B equation should not intersect and all others would.
The program seems to be understanding this, but unless the slope of the inputted line segment is 0 it outputs inf or -inf.
why is this happening?
#include <iostream>
using namespace std;
int main ()
{
typedef double vector2d[2];
vector2d pointA, pointB, pointC, pointD;
double LineSeg1, LineSeg2;
double yes, no;
cout << "Enter x for point A: ";
cin >> pointA[0];
cout << "Enter y for point A: ";
cin >> pointA[1];
cout << "Point A = (" << pointA[0] << "," << pointA[1] << ")" << endl;
cout << "Enter x for point B: ";
cin >> pointB[0];
cout << "Enter y for point B: ";
cin >> pointB[1];
cout << "Point B = (" << pointB[0] << "," << pointB[1] << ")" << endl;
cout << "Enter x for point C: ";
cin >> pointC[0];
cout << "Enter y for point C: ";
cin >> pointC[1];
cout << "Point C = (" << pointC[0] << "," << pointC[1] << ")" << endl;
cout << "Enter x for point D: ";
cin >> pointD[0];
cout << "Enter y for point D: ";
cin >> pointD[1];
cout << "Point D = (" << pointD[0] << "," << pointD[1] << ")" << endl;
LineSeg1 = ((pointB[1]-pointA[1])/(pointB[0]-pointB[0]));
cout << "slope segment 1 = (" << LineSeg1 << endl;
LineSeg2 = ((pointD[1]-pointC[1])/(pointD[0]-pointC[0]));
cout << "slope segment 2 = (" << LineSeg2 << endl;
if ( LineSeg1 == LineSeg2 ) {
cout << "no\n";
}
else ( LineSeg1 != LineSeg2 ) ;{
cout << "yes\n";
}
return 0;
}

This line:
LineSeg1 = ((pointB[1]-pointA[1])/(pointB[0]-pointB[0]));
has a divide by zero error.
I believe the equation should be:
LineSeg1 = ((pointB[1]-pointA[1])/(pointB[0]-pointA[0]));

Related

Incorrect user input

Trying to let only numbers be a vaild input for a quadratic equation solver. I used bool and broke my code and no matter the input it just gives me my error message even if it is a vaild input.
After the progarm ask you to confirm the coefficients are correct, even if you put Y you get the " Please input a number. Please try again". how do you fix this?
I added the bool becasue that what my teacher showed me to use for checking inputs, very new to c++ and coding. I just added the whole code, the HW was to update HW 5 to do a few things more. I broke it trying to check inputs, when adding bool.
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int main()
{
string first, last;
double a, b, c, x1, x2, discriminant, realpart, imaginarypart;
char ch;
cout << "Please enter First and Last name," << endl;
cout << " " << endl;
cout << "First Name= ";
cin >> first;
cout << "and" << endl;
cout << "Last Name=";
cin >> last;
cout << " " << endl;
cout << " Hello " << first << " " << last << " "
<< "welcome." << endl;
cout << " " << endl;
bool isCorrect{ true };
do {
start:
isCorrect = true;
cout << "Enter the coefficients of a: ";
cin >> a;
cout << " " << endl;
cout << "Enter the coefficients of b: ";
cin >> b;
cout << " " << endl;
cout << "Enter the coefficient of c: ";
cin >> c;
cout << " " << endl;
cout << " A = " << a;
cout << " B = " << b;
cout << " C = " << c << endl
<< endl;
cout << " Confirm the coefficients value are correct or not (y/n): " << endl;
cout << " " << endl;
cin >> ch;
if (ch == 'n' || ch == 'N')
goto start;
cout << " " << endl;
discriminant = b * b - 4.0 * a * c;
if (cin.fail())
;
{
isCorrect = false;
cin.clear();
cin.ignore(245, '\n');
cout << " Please input a number. Please try again" << endl;
}
} while (!isCorrect);
bool isExit(false);
if (a == 0) {
cout << " " << endl;
cout << "Error message: a can not = zero (0) " << endl;
}
else if (discriminant > 0) {
x1 = (-b + sqrt(discriminant)) / (2.0 * a);
x2 = (-b - sqrt(discriminant)) / (2.0 * a);
cout << "Roots are real and different." << endl;
cout << " " << endl;
cout << "x1 = " << x1 << endl;
cout << "x2 =" << x2 << endl;
}
else if (discriminant == 0) {
cout << "Roots are real and same." << endl;
cout << " " << endl;
x1 = -b / (2.0 * a);
cout << "x1 = x2 ="
" "
<< x1 << endl;
}
else {
//cout << "Error message: No real solution exist." << endl; // Part 1 error code for no real solutions
realpart = -b / (2.0 * a); //Code for part 2
imaginarypart = sqrt(-discriminant) / (2.0 * a); // Code for part 2
cout << "Roots are complex and different." << endl; // Code for part 2
cout << " " << endl;
cout << "x1 = " << realpart << "+" << imaginarypart << "i" << endl; // Code for part 2
cout << "x2 = " << realpart << "-" << imaginarypart << "i" << endl; // Code for part 2
}
cout << " " << endl;
cout << " Would you like to solve another quadratic equation (y/n): " << endl;
cin >> ch;
if (ch == 'y' || ch == 'Y')
goto start;
else
(ch == 'n' || ch == 'N');
return 0;
}

Dividing the length by sin(x) always results in a negative answer

#include <iostream>
#include <math.h>
#include <stdio.h>
using namespace std;
int main()
{
int caltype;
int numorden;
int length, angle;
cout << "Type '1' for sine, '2' for cosine and '3' for tangent." << endl;
cin >> caltype;
switch (caltype) {
case 1:
cout << "Is the unknown length the numerator or the denominator? Type '1' for numerator and '2' for denominator." << endl;
cin >> numorden;
switch (numorden) {
case 1:
cout << "Type the length of the hypotenuse." << endl;
cin >> length;
cout << "Type in the angle." << endl;
cin >> angle;
cout << "sinangle = O/H" << endl;
cout << "sin" << angle << " = x/" << length << endl;
cout << length << "sin" << angle << " = x" << endl;
cout << "Therefore x = " << length *sin (angle);
break;
case 2:
cout << "Type the length of the opposite." << endl;
cin >> length;
cout << "Type in the angle." << endl;
cin >> angle;
cout << "sinangle = O/H" << endl;
cout << "sin" << angle << " = " << length << "/x" << endl;
cout << "xsin" << angle << " = " << length << endl;
cout << "Therefore x = " << length / sin (angle);
}
}
}
What I am making right now is a program that will show you the steps when it is doing trigonometry. For some reason, when the opposite is divided by, lets say, sin(30), it always results in a negative number. Why is that? The error appears to be occurring at this line:
cout << "Therefore x = " << length / sin (angle);
You need to convert it to radians:
sin (angle * PI/180);
UPDATE:
What is PI?
World Record values of PI
The sin function takes input as radian:
sin (angle * M_PI/180);
where M_PI = 3.141...., which is a constant.

C++ Program need help to debug

#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdlib.h>
using namespace std;
struct football_game
{
string visit_team;
int home_score;
int visit_score;
};
void printMenu();
int main()
{
int i, totalValues = 0;
ifstream inputFile;
string temp = "";
inputFile.open("games.txt");
if (!inputFile)
{
cout << "Error opening Input file!" << endl;
exit(101);
}
inputFile >> totalValues;
getline(inputFile, temp);
cout << " *** Football Game Scores *** " << endl << endl;
cout << " * Total Number of teams : " << totalValues << endl << endl;
football_game* records = new football_game[totalValues];
// while (!inputFile.eof())
// {// == NULL) {
for (i = 0; i < totalValues; i++)
{
getline(inputFile, records[i].visit_team);
cout << records[i].visit_team << endl;
inputFile >> records[i].home_score >> records[i].visit_score;
cout << records[i].home_score << " " << records[i].visit_score << endl;
getline(inputFile, temp);
}
//}
cout << endl;
int choice = 0;
int avg_home_Score = 0;
int avg_visit_Score = 0;
printMenu(); // prints menu
cout << "Please Enter a choice from the Menu : ";
cin >> choice;
cout << endl << endl;
while (true)
{
switch (choice)
{
case 1:
cout << " Score Table " << endl;
cout << " ***********************" << endl << endl;
cout << " VISIT_TEAM"
<< " "
<< " HIGH_SCORE"
<< " "
<< "VISIT_SCORE " << endl;
cout << " -----------"
<< " "
<< "-----------"
<< " "
<< "------------" << endl;
for (int i = 0; i < totalValues; i++)
{
cout << '|' << setw(18) << left << records[i].visit_team << " " << '|'
<< setw(7) << right << records[i].home_score << " " << '|' << setw(7)
<< right << records[i].visit_score << " " << '|' << endl;
}
cout << endl << endl << endl;
break;
case 2:
{
string team_name;
cout << "Enter the Team Name : ";
cin >> team_name;
for (int i = 0; i < totalValues; i++)
{
if (records[i].visit_team == team_name)
{
cout << " VISIT_TEAM"
<< " "
<< " HIGH_SCORE"
<< " "
<< "VISIT_SCORE " << endl;
cout << " -----------"
<< " "
<< "-----------"
<< " "
<< "------------" << endl;
cout << '|' << setw(18) << left << records[i].visit_team << " " << '|'
<< setw(7) << right << records[i].home_score << " " << '|'
<< setw(7) << right << records[i].visit_score << " " << '|'
<< endl;
}
}
cout << endl;
break;
}
case 3:
{
for (int i = 0; i < totalValues; i++)
avg_home_Score += records[i].home_score;
cout << "Average home_score: " << (avg_home_Score / totalValues) << endl << endl;
break;
}
case 4:
{
for (int i = 0; i < totalValues; i++)
avg_visit_Score += records[i].visit_score;
cout << "Average visit_score: " << (avg_visit_Score / totalValues) << endl << endl;
break;
}
default:
{
cout << "Please enter valid input !!" << endl;
break;
}
}
printMenu();
cin >> choice;
}
return 0;
}
void printMenu()
{
cout << " Menu Options " << endl;
cout << " ================ " << endl;
cout << " 1. Print Information of all Games[Table Form] " << endl;
cout << " 2. Print Information of a Specific Game " << endl;
cout << " 3. Print Average points scored by the Home Team during season" << endl;
cout << " 4. Print Average points scored against the Home Team" << endl << endl << endl;
}
Here is the input file i am using
games.txt
5
SD Mines
21 17
Northern State
10 3
BYU
10 21
Creighton
14 7
Sam Houston State
14 24
When i am using the 2nd option (Print Information of a Specific Game) from the output screen,
it ask me to enter the team name and when i enter the team-name.
For example: SD Mines it gives me an error, but when I enter the team-name with no space like: BYU it works fine for me.
cin >> team_name;
Takes the input only upto space.
You might want to use cin.getline() for taking space separated strings as input.
A small program demonstrating the same :
#include <iostream>
#include <string>
int main ()
{
std::string name;
std::cout << "Please, enter your full name: ";
std::getline (std::cin,name);
std::cout << "Name is : , " << name << "!\n";
return 0;
}
std::cin ignores whitespaces by default.
To include spaces in your input try :
getline(cin, team_name);
This would pick up all the characters in a line until you press enter. This is available in
#include<string>
You need to flush the std::cin buffer after reading the choice:
#include <limits>
//...
cin >> choice;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
Refer to this question for detailed explanation.
Also, if you want to read strings with spaces from the standard input, replace this:
cin >> team_name;
with this:
getline(cin, team_name);
as already mentioned in other answers. No need to flush std::cin this time, since you have already read the full line.
Finally, remove extra newlines from your games.txt:
5
SD Mines
21 17
Northern State
...

C++ functions and loops

I have to write a program that simulates an ice cream cone vendor. The user inputs the number of cones, and for each cone, the user inputs the number of scoops, then the flavor(a single character) for each scoop. At the end, the total price is listed. For the pricing, 1 scoop costs 2.00, 2 scoops costs 3.00 and each scoop after 2 costs .75.
I'm having trouble with the pricing. The correct price is displayed if the user only wants one cone.
/*
* icecream.cpp
*
* Created on: Sep 14, 2014
* Author:
*/
#include <iostream>
#include <string>
using namespace std;
void welcome() {
cout << "Bob and Jackie's Ice Cream\n";
cout << "1 scoop - $1.50\n";
cout << "2 scoops - $2.50;\n";
cout << "Each scoop after 2 - $.50\n";
cout << "Ice Cream Flavors: Only one input character for each flavor.\n";
}
bool checkscoops(int scoops) {
int maxscoops = 5;
if ((scoops > maxscoops) || (scoops < 1))
return false;
else
return true;
}
bool checkcones(int cones) {
int maxcones = 10;
if ((cones > maxcones) || cones < 1)
return false;
else
return true;
}
int price(int cones, int numberofscoops) {
float cost = 0.00;
{
if (numberofscoops == 5) {
cost = cost + 5 + (.75 * 3);
}
if (numberofscoops == 4) {
cost = cost + 5 + (.75 * 2);
}
if (numberofscoops == 3) {
cost = cost + 5.75;
}
if (numberofscoops == 2) {
cost = cost + 5.00;
}
if (numberofscoops == 1) {
cost = cost + 2.00;
}
}
cout << "Total price is: " << cost << endl;
}
int buildcone(int numcones) {
char flav1, flav2, flav3, flav4, flav5;
int numberofscoops;
for (int i = 1; i <= numcones; i++) {
cout << "Enter the amount of scoops you wish to purchase. (5 max): ";
cin >> numberofscoops;
checkscoops(numberofscoops);
while (checkscoops(numberofscoops) == false) {
cout << "You are not allowed to buy more than 5 scoops and you "
"cannot buy less than one scoop. Please try again.\n";
cout << "How many scoops would you like?(5 max): ";
cin >> numberofscoops;
checkcones(numberofscoops);
}
cout << "You are buying " << numberofscoops
<< " scoops of ice cream.\n";
if (numberofscoops == 5) {
cout << "Enter flavor 1: ";
cin >> flav1;
cout << "Enter flavor 2: ";
cin >> flav2;
cout << "Enter flavor 3: ";
cin >> flav3;
cout << "Enter flavor 4: ";
cin >> flav4;
cout << "Enter flavor 5: ";
cin >> flav5;
cout << " ( " << flav1 << " )/" << endl;
cout << " ( " << flav2 << " )" << endl;
cout << " ( " << flav3 << " )" << endl;
cout << " ( " << flav4 << " )" << endl;
cout << " ( " << flav5 << " )" << endl;
cout << " \\"
<< " /" << endl << " |" << endl;
}
if (numberofscoops == 4) {
cout << "Enter flavor 1: ";
cin >> flav1;
cout << "Enter flavor 2: ";
cin >> flav2;
cout << "Enter flavor 3: ";
cin >> flav3;
cout << "Enter flavor 4: ";
cin >> flav4;
cout << " ( " << flav1 << " )" << endl;
cout << " ( " << flav2 << " )" << endl;
cout << " ( " << flav3 << " )" << endl;
cout << " ( " << flav4 << " )" << endl;
cout << " \\"
<< " /" << endl << " |" << endl;
}
if (numberofscoops == 3) {
cout << "Enter flavor 1: ";
cin >> flav1;
cout << "Enter flavor 2: ";
cin >> flav2;
cout << "Enter flavor 3: ";
cin >> flav3;
cout << " ( " << flav1 << " )" << endl;
cout << " ( " << flav2 << " )" << endl;
cout << " ( " << flav3 << " )" << endl;
cout << " \\"
<< " /" << endl << " |" << endl;
}
if (numberofscoops == 2) {
cout << "Enter flavor 1: ";
cin >> flav1;
cout << "Enter flavor 2: ";
cin >> flav2;
cout << " ( " << flav1 << " )" << endl;
cout << " ( " << flav2 << " )" << endl;
cout << " \\"
<< " /" << endl << " |" << endl;
}
if (numberofscoops == 1) {
cout << "Enter a flavor: ";
cin >> flav1;
cout << " ( " << flav1 << " )" << endl;
cout << " \\"
<< " /" << endl << " |" << endl;
}
}
price(numcones, numberofscoops);
}
int main() {
int numberofcones;
int numberofscoops;
welcome();
cout << "How many cones would you like?(10 max) ";
cin >> numberofcones;
checkcones(numberofcones);
while (checkcones(numberofcones) == false) {
cout << "You are not allowed to buy more than 10 cones and you cannot "
"buy less than one cone. Please try again.\n";
cout << "How many cones would you like?(10 max): ";
cin >> numberofcones;
checkcones(numberofcones);
}
cout << "You are buying " << numberofcones << " ice cream cones.\n";
buildcone(numberofcones);
}
Start by changing the return value of price() to float, or the function won't be able to return the proper cost. Also, since cones is not used to compute the cost of the purchase, we don't it as a parameter:
float price(int numberofscoops)
{
float total_cost = 0.0f;
if (numberofscoops == 1) {
total_cost = 2.0f;
}
else if (numberofscoops == 2) {
total_cost = 3.0f;
}
else if (numberofscoops > 2) {
total_cost = 5.0f + ((numberofscoops-2) * 0.75f);
}
return total_cost;
}
You code could have other problems, but I think these changes will let you continue to debug and fix the code on your own.
Your while() loop is flawed. Comment your call to checkcones() as shown below. You're already calling checkcones() as the conditional in your while(), no need to evaluate again as this will sent you into a perma-loop. You've got two of these while() statements that I could see, you'll want to comment out both.
while ( checkcones( numberofcones ) == false )
{
cout << "You are not allowed to buy more than 10 cones and you cannot buy less than one cone. Please try again.\n";
cout << "How many cones would you like?(10 max): ";
cin >> numberofcones;
// THIS LINE IS THE PROBLEM :)
// checkcones(numberofcones);
}
After this fix, your program begins to work but the pricing fails. You should be able to figure that out with the answer given above.
I would also see if you can figure out how to implement a c++ class with members and methods as your current approach is very "c" like. Happy coding! :)

Line 48 and 62 C++ error Expected unqualified id before if?

I am programming a simple calculator that subtracts adds divides or multiplies depending on whether you type 1 2 3 or 4. I keep getting this error. Keep in mind I am newer to C++. It happens with the IF lines in Mode == 3 and Mode == 4
#include <iostream>
int main(){
using namespace std;
int x;
int y;
int x2;
int y2;
int x3;
int y3;
int x4;
int y4;
int Mode;
cout << "Welcome to Brian's Calculator!";
cout << endl;
cout << "Pick a mode. 1 is Addition. 2 Is Subtraction. 3 is Multiplacation. 4 is Division";
cin >> Mode;
if (Mode==1){
cout << "You chose addition.";
cout << endl;
cout << "Pick a number.";
cout << endl;
cin >> x;
cout << endl;
cout << "Pick another.";
cout << endl;
cin >> y;
cout << "The sum of the numbers you chose are: " << x+y <<".";
return 0;
};
if (Mode==2){
cout << "You chose subtraction.";
cout << endl;
cout << "Pick a number.";
cout << endl;
cin >> x2;
cout << endl;
cout << "Pick another.";
cout << endl;
cin >> y2;
cout << "The difference of the numbers you chose are: " << x2-y2 <<".";}
return 0;
};
if (Mode==3){
cout << "You chose Multiplacation.";
cout << endl;
cout << "Pick a number.";
cout << endl;
cin >> x3;
cout << endl;
cout << "Pick another.";
cout << endl;
cin >> y3;
cout << "The product of the numbers you chose are: " << x3*y3 <<".";
return 0;
};
if (Mode==4){
cout << "You chose Division.";
cout << endl;
cout << "Pick a number.";
cout << endl;
cin >> x4;
cout << endl;
cout << "Pick another.";
cout << endl;
cin >> y4;
cout << "The quotient of the numbers you chose are: " << x4/y4 <<".";
return 0;
};
At this line:
cout << "The difference of the numbers you chose are: " << x2-y2 <<".";}
You have an extra }
PROBLEM: you have two end-braces instead of one:
if (Mode==2){
...
// DELETE THE EXTRANEOUS "}"!
cout << "The difference of the numbers you chose are: " << x2-y2 <<".";}
return 0;
};
SUGGESTED ALTERNATIVE:
if (Mode==2){
...
// DELETE THE EXTRANEOUS "}"!
cout << "The difference of the numbers you chose are: " << x2-y2 <<".";
return 0;
}
EVEN BETTER:
switch (Mode) {
case 1 :
...
break;
case 2 :
...
break;
On this line you have a misplaced }:
cout << "The difference of the numbers you chose are: " << x2-y2 <<".";}
^
when you fix that you will need an extra } after the end of your program to close main as well. Also you do not need a ; after the } when you close the if statements for example:
if (Mode==1){
// code...
};
^