I am -trying- to use nested structs/structures, and after several hours of pseudocode and attempts, the final result that I come up with doesn't work or doesn't compile.
I would like to take two vectors A and B, and compare them against each other. I set up nested struct to read the start and end point of the vector, and the vector struct itself. So I think I may be doing some wrong further below, but I am stuck.
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
struct Point // Reads in three coordinates for point to make a three dimensional vector
{
double x;
double y;
double z;
};
struct MathVector // Struct for the start and end point of each vector.
{
Point start;
Point end;
};
Point ReadPoint()
{
Point pt; // Letter distinguishes between vector A and vector B, or "letterA" and "letterB"
double x, y, z;
cout << "Please input the x-coordinate: " << endl;
cin >> pt.x;
cout << "Please input the y-coordinate: " << endl;
cin >> pt.y;
cout << "Please input the z-coordinate: " << endl;
cin >> pt.z;
return pt;
}
void DotProduct (MathVector letterA, MathVector letterB, double& a_times_b ) //formula to compute orthogonality
{
a_times_b = (letterA.end.x - letterA.start.x)*(letterB.end.x - letterB.start.x) + (letterA.end.y - letterA.start.y)*(letterB.end.y - letterB.start.y) + (letterA.end.z - letterA.start.z)*(letterB.end.z - letterB.start.z);
}
int main()
{
MathVector letterA;
MathVector letterB;
double a_times_b;
letterA = ReadPoint();
letterB = ReadPoint();
DotProduct (letterA, letterB, a_times_b);
cout << "The vector " << letterA << " compared with " << letterB << " ";
if ( a_times_b == 0)
cout << "is orthoganal." << endl;
else
cout << "is not orthoganal." << endl;
return 0;
}
One problem is with your ReadPoint whose return type is Point, but you're returning an instance of MathVector. Also, you read the input into variables which ignore eventually.
You should write ReadPoint as:
Point ReadPoint()
{
Point p;
cout << "Please input the x-coordinate: " << endl;
cin >> p.x;
cout << "Please input the y-coordinate: " << endl;
cin >> p.y;
cout << "Please input the z-coordinate: " << endl;
cin >> p.z;
return p;
}
Or a little better version:
Point ReadPoint()
{
Point p;
cout << "Please enter point-coordinate : " << endl;
cin >> p.x >> p.y >> p.z; //example input : 1 2 3
return p;
}
Or, still better is, overload >> operator as:
std::istream & operator>>(std::istream & in, Point & p)
{
cout << "Please enter point-coordinate : " << endl;
return cin >> p.x >> p.y >> p.z; //example input : 1 2 3
}
//Use this as
Point pointA, pointB;
cin >> pointA >> pointB;
Now read a good C++ book. If you're already reading one, then make sure it is really good. Here is a list of really good C++ books, of all levels:
The Definitive C++ Book Guide and List
ReadPoint returns letter of type MathVector instead of Point
You haven't overloaded operator << to tell it how to handle MathVector objects
letterA and letterB are of type MathVector
MathVector letterA;
MathVector letterB;
double a_times_b;
letterA = ReadPoint();
letterB = ReadPoint();
you should create another method to read Mathvector.. as you are doing with Point.
and in method ReadPoint
return type must be Point .. If you reading point then do calculation here to create the object of MathVector go tet startpoint and endpoint format.
Point ReadPoint()
{
MathVector letter; // Letter distinguishes between vector A and vector B, or "letterA" and "letterB"
double x, y, z;
cout << "Please input the x-coordinate: " << endl;
cin >> x;
cout << "Please input the y-coordinate: " << endl;
cin >> y;
cout << "Please input the z-coordinate: " << endl;
cin >> z;
return letter;
}
You didn't explain what it is you're trying to do or what errors you got, but this code makes no sense. You have three variables, x, y, and z. You fill them with values you get from the user. Then you don't do anything with those variables and return the MathVector created by a default constructor even though you say you're going to return a Point. That makes very little sense.
No match for 'operator=' error means that there's no function for assigning a MathVector to a Point. You are calling ReadPoint() which returns a Point and trying to assign the returned value to a variable of type MathVector. The compiler can't create a 'convertion' function automatically. You have to provide one yourself. Perhaps what you meant was
letterA.start = ReadPoint();
letterA.end = ReadPoint();
Related
I am having this problem with my calculator I made. See, when I type in a calculation it always adds a 0 to the end. I don't know how to fix this do you have any ideas?
Here's the code:
#include <iostream>
using namespace std;
void Input(float &x, float &y);
float a = 1.0, b = 1.0, result;
char op;
int main() {
cout << "Welcome to Foxy's calculator" << endl;
cout << "----------------------------" << endl;
cout << "Please input a calculation operation (eg. 1+1): ";
cin >> a >> op >> b;
Input(a, b);
cout << result << endl;
system("pause");
return 0;
}
void Input (float &x, float &y) {
a = x;
b = y;
switch (op)
{
case '+':
cout << x + y;
break;
case '-':
cout << x - y;
break;
case '*':
cout << x*y;
break;
case '/':
cout << x / y;
break;
default:
cout << "Error! Operator is not correct" << endl;
cout << "Please input your calculation with a proper operator: ";
cin >> a >> op >> b;
}
}
result is a global static variable that gets zero - initialized and is never changed. So cout << result << endl; will always print "0". To fix this you should make a, b, result and op local to main (global variables are bad), pass a, b andop to calculating function and store returned calculation result in result. It will look something like this:
float result = Input(a, b, op);
cout << result << endl;
You call cout << result << endl; in the caller. and result is always 0. This is because it is never explicitly set to anything and the C++ compiler kindly zero-initialises it since it's at global scope.
In such instances, your line by line debugger is your best friend. The fact that you've mashed up your 1) input, 2) calculation, and 3) output stages is not helping: ideally they should all be separate parts of your program.
Remove cout << result << endl;
I'm new to C++ and stack overflow in general so please excuse me if a make a mistake somewhere.
I posted my code down below, but my issue is that when I type either yes or no at after the calculation is complete, no is supposed to end the program (which I was still working on) and yes is supposed to set it up for another calculation.
However I end up with a glitchy loop.
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
bool b;
bool yes = b;
do {
float x;
float y;
float z;
float a;
cout << "Enter The amount you are investing:" << endl;
cin >> x;
cout << "Enter the rate:" << endl;
cin >> y;
cout << "Enter the investment period (years):" << endl;
cin >> z;
cout << "Enter the compounding period:" << endl;
cin >> a;
cout << pow((1 + y / a), (a*z))*x << endl << "Want to do another? (yes/no)";
cin >> b;
cin.ignore();
} while (yes = true); {
cin.clear();
if (b = yes) {
}
else {
}
}
return 0;
}
The behaviour of your code is probably due to:
unintentional reassignment of the termination condition bool value: yes to: true, instead of checking its value, which is done with ==, not with the assignment =.
no modification of the value yes within the while loop.
A possible update is:
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
// initialise the sentinel
bool yes = true;
do {
// define variables
float x, y, z, a;
// read input
cout << "Enter The amount you are investing:" << endl;
cin >> x;
cout << "Enter the rate:" << endl;
cin >> y;
cout << "Enter the investment period (years):" << endl;
cin >> z;
cout << "Enter the compounding period:" << endl;
cin >> a;
cout << pow((1 + y / a), a * z) * x << endl;
// redo calculation or exit
cout << "Want to do another? (yes/no)";
cin >> yes;
// check termination condition
} while (yes == true);
return 0;
}
Additionally, watch out for the uninitialised variables: x, y, z, a and think for a proper default value that will indicate possible wrong result.
Lastly, withing the calculation: 1 + y / a is ambiguous, it could mean both: (1 + y) / a and: 1 + (y / a), put parentheses to enforce precedence in the wanted order.
You are not modifying the value of variable yes. It is always set to true.
Hello I have class point and the job is to make array of points (objects of the same class), but the class has more than one constructor. How to declare which one of them I want in my array? The code:
class point
{
private:
double pointX, pointY;
string color;
int form;
public:
point();
point(double, double, string color = "red", int form = 2);
point(string color = "red", int form = 2);
~point() {
cout << "Deleting object point" << endl;
}
inline void print();
inline void distance();
};
point::point(double x, double y, string color = "red", int form = 2) {
cout << "enter x coordinate of the point x = "; cin >> pointX;
cout << "enter y coordinate of the point y = "; cin >> pointY;
}
point::point(string color = "red", int form = 2) {
cout << "enter x coordinate of the point x = "; cin >> pointX;
cout << "enter y coordinate of the point y = "; cin >> pointY;
}
point::point() {
cout << "enter x coordinate of the point x = "; cin >> pointX;
cout << "enter y coordinate of the point y = "; cin >> pointY;
cout << "enter color of the point "; getline(cin, color);
cout << "enter number form 1 - 3 for the form of the point "; cin >> form;
}
inline void point::print() {
cout << "the x coordinate of the point is x = " << pointX << endl;
cout << "the y coordiante of the point is y = " << pointY << endl;
cout << "the color of the point is " << color << endl;
if (form = 1) cout << "the form is circle" << endl;
if (form = 2) cout << "the form is square" << endl;
if (form = 2) cout << "the form is cross" << endl;
}
inline void point::distance() {
double z;
z = sqrt(pointX*pointX + pointY*pointY);
cout << "distance between the point and the start of coordinate system is " << z << endl;
}
double pointDistatnce() {
double z, x, y;
point points = new point[4];
}
!!! point points = new point[4];` // here must be the array of objects but it shows me error that "class point has more than one default constructor"?
And I want to use the constructor without parameters so for the user to construct his own point. Here are the errors from the error list;
!! no suitable constructor exists to convert from "point
!! class "point" has more than one default constructor
point(); declares a default constructor. So does point(string color = "red", int form = 2); Either get rid of the first one and just use the second, or get rid of the default value for color in the second.
Edit based on the extra info you posted: You are looking for the compiler to read the programmer's mind. You defined a constructor with no parameters that does one thing, but you defined a constructor with two optional parameters that does a different thing. When the constructor is invoked and given no parameters, does that mean it should execute with no parameters or does it mean it should execute with the default values for the two optional parameters.
None of that changes my original answer. It just means you need to think a bit in order to use that answer. In the partial code you initially provided, one might hope that the two constructors that each could be invoked with no parameters were redundant with each other, so just dropping the redundancy would fix it. But since you want the programmer to be able to invoke one of two different constructors, you need to think of a way to tell the compiler which one should be used.
While rethinking your design, you should also try to drop the idea of using cin within a constructor. It is not technically wrong. But it is a bad enough idea that you shouldn't do it.
I've been assigned a problem that asks us to solve a 2 equation system using an array and a pointer to that array. It's sort of a linear algebra way of going about it, with x_1 = (DE-BF)/(AD - BC) and x_2 = (AF - CE)/(AD - BC). The system is Ax_1 + Bx_2 = C and Dx_1 + Ex_2 = F. My code compiles fine but spits out garbage. Can anyone help me? I'm sure it's an error with my pointers but I don't know how to correct it. Much thanks in advance.
#include "stdafx.h"
#include <iostream>
using namespace std;
int main(){
double A,B,C,D,E,F;
cout << "Please enter a value for A: " << endl;
cin >> A;
cout << "Please enter a value for B: " << endl;
cin >> B;
cout << "Please enter a value for C: " << endl;
cin >> C;
cout << "Please enter a value for D: " << endl;
cin >> D;
cout << "Please enter a value for E: " << endl;
cin >> E;
cout << "Please enter a value for F: " << endl;
cin >> F;
double paramarray[6] = {A,B,C,D,E,F};
double* p;
p = ¶marray[6];
double x1 = (p[3]*p[4] - p[1]*p[5])/(p[0]*p[3] - p[1]*p[2]);
double x2 = (p[0]*p[5] - p[2]*p[4])/(p[0]*p[3] - p[1]*p[2]);
cout << "X_1 = " << x1 << endl;
cout << "X_2 = " << x2 << endl;
int f;
cin >> f;
return 0;
}
p = ¶marray[6];
This is the problem. This means you are assigning the address of paramarray[6] to p. paramarray[6] is not defined and you are trying to access out of bounds array.
Try changing it to
p = paramarray;
Also, it will be better if you first check for zero denominator and update your equation accordingly.
Your pointer should be initialized with the base of the array which is the address of the first element. And in your program you are initializing it to a address out of bounds which is the index 6 where the last index of the array is 5 itself.
An array of size six means the first index is 0 and the last index is 5.
so change your line:
p = ¶marray[6];
to
p = paramarray; //or p=¶marray[0].Both are same here
This above line will store the address of the first element in pointer p.
I have here a code that is supposed to ask the user two sets of real and imaginary numbers.
#include <iostream>
using namespace std;
class Complex {
public:
double r;
double i;
public:
Complex();
void add(Complex, Complex);
void subtract(Complex, Complex);
void print();
};
Complex::Complex() {
r = i = 0;
}
void Complex::add (Complex op1, Complex op2) {
r = op1.r+op2.r;
i = op1.i+op2.i;
}
void Complex::subtract (Complex op1, Complex op2) {
r = op1.r-op2.r;
i = op1.i-op2.i;
}
void Complex::print () {
cout << r << i;
}
int main () {
Complex operand1, operand2, result;
cout << "Input real part for operand one: " << endl;
cin >> operand1.r;
cout << "Input imaginary part for operand one: " << endl;
cin >> operand1.i;
cout << "Input real part for operand two: " << endl;
cin >> operand2.r;
cout << "Input imaginary part for operand two: " << endl;
cin >> operand2.i;
result.add(operand1, operand2);
cout << "The sum is " << result.add << endl;
result.subtract(operand1, operand2);
cout << "The difference is " << result.subtract << endl;
}
However, when I compiled the program, lots of errors are displayed (std::basic_ostream) which I don't even get.
Another issue I'm having is in the function void::Complex print. There should be a condition inside cout itself. No if-else. But I have no idea what to do.
The program must run like this:
Input real part for operand one: 5
Input imaginary part for operand one: 2 (the i for imaginary shouldn't be written)
Input real part for operand two: 8
Input imaginary part for operand two: 1 (again, i shouldn't be entered)
/then it will print the input(ed) numbers/
(5, 2i) //this time with an i
(8, 1i)
/then the answers/
The sum is 13+3i.
The difference is -3, 1i. //or -3, i
Please help me! I'm new in C++ and here in stackoverflow and your help would be very appreciated. Thank you very much!
The line
cout << "The sum is " << result.add << endl;
is incorrect, as add is a method so result.add will be a pointer to that method, and cout does not know how to handle it - which makes the compiler spit it out.
Change the line to
cout << "The sum is ";
result.print();
cout << endl;
You need to do the same for the line
cout << "The difference is " << result.subtract << endl;
As to coding style, the two methods are overwrting an existing complex number. Perhaps having a the function like this would be better
Complex &Complex::add (const Complex &op) {
r += op.r;
i += op.i;
return *this;
}
This will enable you to chain additions together and also just add a complex number to the existing complex number.
In addition you could make the class variables r and i private. This will require an alternative constructor:
Complex:Complex(double real, double imaginary) : r(real), i(imaginary) {};
Finally you may wish to consider operator overloading - I am sure you can google that to find a reasonable tutorial.
In main, after you call result.add, you put the same function in the cout stream when it doesn't return anything. I think you meant to write cout << "the sum is " << result.print();
You are already using the std:: namespace. Just use the complex number library in it like this answer suggests: Addition of complex numbers using classes
#include <iostream>
using namespace std;
class Complex {
public:
double r;
double i;
public:
void add(Complex, Complex);
void subtract(Complex, Complex);
void print();
};
void Complex::add (Complex op1, Complex op2) {
r = op1.r + op2.r;
i = op1.i + op2.i;
}
void Complex::subtract (Complex op1, Complex op2) {
r = op1.r - op2.r;
i = op1.i - op2.i;
}
void Complex::print () {
cout << "("<<r<<", " << i <<")";
}
int main () {
Complex operand1, operand2, result;
cout << "\nInput real part for operand one: " << endl;
cin >> operand1.r;
cout << "Input imaginary part for operand one: " << endl;
cin >> operand1.i;
cout << "Input real part for operand two: " << endl;
cin >> operand2.r;
cout << "Input imaginary part for operand two: " << endl;
cin >> operand2.i;
cout << "\nThe sum is ";
result.add(operand1, operand2);
result.print();
cout << "\nThe difference is ";
result.subtract(operand1, operand2);
result.print();
}