C++ My program is in infinite loop - visual-c++

I appreciate you taking the time to read my problem sorry I'm just a beginner in programming. Apparently I am not that good in loops and if else statement and I don't know whats wrong in my code. when I run it and input "up" its just go to (8,10). it should have been (0,1) then the console will ask which direction they want to go again. Thanks in advance!!! :D
#include<iostream>;
#include<string>;
using namespace std;
int main()
{
int x = 0;
int y = 0;
string input;
cout << "objective: go to (8,9)" << endl;
cout << "current location (" << x << "," << y << ")" << endl;
while ( (x = 8) && (y = 9) )
{
cout << "which way do you want to go?: ";
cin >> input;
if (input == "up")
{
++y;
cout << "current location (" << x << "," << y << ")" << endl;
break;
}
else if (input == "down")
{
--y;
cout << "current location (" << x << "," << y << ")" << endl;
break;
}
else if (input == "right")
{
++x;
cout << "current location (" << x << "," << y << ")" << endl;
break;
}
else if (input == "left")
{
--x;
cout << "current location (" << x << "," << y << ")" << endl;
break;
}
}
cout << "current location (" << x << "," << y << ")" << endl;
cout << "Congratulations! You have reach your destination! :D" << endl;
system("pause");
return(0);
}

"
I tried removing all the break but it went straight down to "congratulations! you have reach your destination" when I tried to run it and I change the condition to (x ==8) && (y ==9) still no improvement.
"
Your loop condition is incorrect. The while loop will only run if x is 8 and y is 9. They are 0 and 0 at the beginning of the program, which explains why we skip the loop. The real condition is: "run the loop as long as x and y are not these values." while (x != 8 || y != 9) and alternately alternately: while (!(x == 8 && y == 9))

Related

Numerical Integration with Unspecified Parameter in C++

I have written a simple code to calculate an integral numerically with both the midpoint and trapezoidal rule. It works fine, but now I would like to modify it so that it can calculate the same integral but with an unspecified parameter. So instead of the integral of e^(-x^2), I would like to calculate the integral of e^(-\alpha*x^2). However, I am stuck here. I tried to just declare a new double alpha without initializing it, but that clearly takes me no where. Is there a way to do such a thing in C++? Thank you in advance.
//=============================================================
// A simple program to numerically integrate a Gaussian over
// the interval a to b
//=============================================================
#include <iostream>
#include <cmath>
using namespace std;
// function implementing the midpoint rule
double midpoint(double a, double b, int n){
double sum = 0.0;
double width = (b-a)/n;
for (int i = 1; i < n; i++){
sum += exp((-1)*(a+(i+0.5)*width)*(a+(i+0.5)*width))*width;
}
return sum;
}
//function implementing the trapezoidal rule
double trapezoidal(double a, double b, int n){
double width = (b-a)/n;
double sum = (width/2)*(exp(-a*a)+exp(-b*b));
for (int i = 1; i < n; i++){
sum += 2*exp((-1)*(a+i*width)*(a+i*width))*width/2;
}
return sum;
}
int main(){
cout << "THIS IS A SIMPLE PROGRAM TO INTEGRATE A GAUSSIAN OVER A CERTAIN INTERVAL." << endl;
int n;
double a, b;
cout << "Enter the lower and upper limit of integration as doubles" << endl;
cin >> a >> b;
cout << "Enter the number of partitions" << endl;
cin >> n;
double actual = 0.886207;
double midRule = midpoint(a,b,n);
double trapRule = trapezoidal(a,b,n);
cout << "For the function e^(-x^2) integrated from "<< a << " to " << b << endl;
cout << "The analytic value of the integral is: " << actual << endl;
cout << "The midpoint value of the integral for " << n << " partitions is: " << midRule << endl;
cout << "The trapezoidal value of the integral for " << n << " partitions is: " << trapRule << endl;
cout << "The percent error for the midpoint is: " << (abs(actual-midRule)/actual)*100 << "%" << endl;
cout << "The percent error for the trapezoidal is: " << (abs(actual-trapRule)/actual)*100 << "%" << endl;
return 0;
}

Expression must have pointer-to-object type Error?

[Updated Code Pictutre][1]Can someone help me understand what I am doing wrong with my array in this code? It is giving me the same error for all my variables of the array
I dont know how to show you the whole code without adding like 5 pictures, i tried to capture all the important information
Ok I have posted the code here below
#include <iostream>
#include <math.h>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
int main() {
// output for a single input driving frequency
double m, c1, c2, k1, k2, r, Y;
m = c1 = c2 = k1 = k2 = r = Y = 0;
//input values for the case
cout << "Mass: ";
cin >> m;
cout << "c1: ";
cin >> c1;
cout << "c2: ";
cin >> c2;
cout << "k1: ";
cin >> k1;
cout << "k2: ";
cin >> k2;
cout << "r: ";
cin >> r;
cout << "Y: ";
cin >> Y;
//solve for the equivlent values
double ceq, keq, wn, dampratio, staticamp;
ceq = c1 + c2;
keq = k1 + k2;
wn = 0;
wn = sqrt(keq / m);
dampratio = ceq / (2 * sqrt(m*keq));
//Determine the Case
double cas = 0;
cout << "Which Case (1-5): ";
cin >> cas;
double Fo, alpha, w, t;
if (cas == 2) {
Fo = Y*k2;
}
else {
w = r*wn;
t = 0;
Fo = 0;
Fo = Y*sqrt((k2*k2 + pow((c2*w), 2)));
alpha = atan(-c2*w / k2);
}
//Static amplitude
staticamp = 0;
staticamp = Fo / keq;
double Xp = (Fo / keq) / (sqrt(pow((1 - r*r), 2) + pow((2 * dampratio*r), 2)));
double phi = atan((ceq*w) / (keq - m*w*w));
double xp = Xp*sin(w*t - phi);
double H, Td, Ftwall, Ftbase;
//Displacement Transmissiblity
Td = 0;
Td = (Fo / (Y*keq)) / (sqrt(pow((1 - r*r), 2) + pow((2 * dampratio*r), 2)));
//Frequanecy Response
H = 0;
H = 1 / (sqrt(pow((1 - r*r), 2) + pow((2 * dampratio*r), 2)));
//Force Transmissibilty to the bang
Ftbase = 0;
Ftbase = (Xp*sqrt(pow((k1 - keq*r*r), 2) + pow((c1*wn*r), r))) / (Y*k2);
//Force Transmissibilty to the wall
Ftwall = 0;
Ftwall = (Xp*sqrt((k1*k1) + pow((c1*r*wn), 2))) / (Y*k2);
if (cas == 5) {
cout << "\nCase: " << cas << endl << endl;
cout << "Wn: " << wn << endl;
cout << "Damping Ratio: " << dampratio << endl;
cout << "Amplitude: " << staticamp << endl;
cout << "Frequency Response: " << H << endl;
cout << "Phase Angle: " << phi << endl;
cout << "Displacement Transmissibilty: " << Td << endl;
cout << "Force Tranmissibility to the base: " << Ftbase << endl;
}
else {
cout << "\nCase: " << cas << endl << endl;
cout << "Wn: " << wn << endl;
cout << "Damping Ratio: " << dampratio << endl;
cout << "Amplitude: " << staticamp << endl;
cout << "Frequency Response: " << H << endl;
cout << "Phase Angle: " << phi << endl;
cout << "Displacement Transmissibilty: " << Td << endl;
cout << "Force Tranmissibility to the base: " << Ftbase << endl;
cout << "Force Transmissibilty to the wall: " << Ftwall << endl;
}
//*****************************Section 1.2 **********************************************
// 1.2 output for a range of driving frequencies
//input values for the case
cout << "Mass: ";
cin >> m;
cout << "c1: ";
cin >> c1;
cout << "c2: ";
cin >> c2;
cout << "k1: ";
cin >> k1;
cout << "k2: ";
cin >> k2;
cout << "Y: ";
cin >> Y;
//solve for the equivlent values
ceq = keq = 0;
ceq = c1 + c2;
keq = k1 + k2;
wn = 0;
wn = sqrt(keq / m); //natural frequency
dampratio = ceq / (2 * sqrt(m*keq)); // damping ratio
//Determine the Case
cas = 0;
cout << "Which Case (1-5): ";
cin >> cas;
double staticamp[200], Xp[200], xp[200], Fo[200], phi[200], w[200];
double alpha[200], Td[200], H[200], Ftbase[200], Ftwall[200], r[200];
r[1] = .1;
for (int i = 0; i<200; i++) {
if (cas == 2) {
Fo = Y*k2;
}
else {
w = r[i] * wn;
t = 0;
Fo = 0;
Fo = Y*sqrt((k2*k2 + pow((c2*w), 2)));
alpha[i] = atan(-c2*w / k2);
}
//Static amplitude
staticamp[i] = 0;
staticamp[i] = Fo / keq;
Xp[i] = (Fo / keq) / (sqrt(pow((1 - r[i] * r[i]), 2) + pow((2 * dampratio*r[i]), 2)));
phi[i] = atan((ceq*w) / (keq - m*w*w));
xp[i] = Xp*sin(w*t - phi);
//Displacement Transmissiblity
Td[i] = 0;
Td[i] = (Fo / (Y*keq)) / (sqrt(pow((1 - r[i] * r[i]), 2) + pow((2 * dampratio*r[i]), 2)));
//Frequanecy Response
H[i] = 0;
H[i] = 1 / (sqrt(pow((1 - r[i] * r[i]), 2) + pow((2 * dampratio*r[i]), 2)));
//Force Transmissibilty to the bang
Ftbase[i] = 0;
Ftbase[i] = (Xp*sqrt(pow((k1 - keq*r[i] * r[i]), 2) + pow((c1*wn*r[i]), r[i]))) / (Y*k2);
//Force Transmissibilty to the wall
Ftwall[i] = 0;
Ftwall[i] = (Xp*sqrt((k1*k1) + pow((c1*r[i] * wn), 2))) / (Y*k2);
//increment r
r[i + 1] = r[i] + .1;
}
ofstream Data(" 1.2 Case 1 .txt"); // File Creation to import in text file to graph
for (int i = 0; i > 200; i++) { //NEED TO CHANGE FILE NAME FOR NEW DATA SET
Data << r[i] << "," << H[i] << "," << phi[i] << "," << Td[i] << "," << Ftbase[i] << "," << Ftwall[i] << endl;
}
//************************* Section 2 ****************
cout << "\t\tSection 2" << endl << endl;
//input values for the case
cout << "Mass: ";
cin >> m;
cout << "c1: ";
cin >> c1;
cout << "c2: ";
cin >> c2;
cout << "k1: ";
cin >> k1;
cout << "k2: ";
cin >> k2;
cout << "r: ";
cin >> r;
cout << "Y: ";
cin >> Y;
//solve for the equivlent values
ceq = c1 + c2;
keq = k1 + k2;
wn = 0;
wn = sqrt(keq / m);
dampratio = ceq / (2 * sqrt(m*keq));
//Determine the Case
cout << "Which Case (1-5): ";
cin >> cas;
if (cas == 2) {
Fo = Y*k2;
}
else {
w = r*wn;
t = 0;
Fo = 0;
Fo = Y*sqrt((k2*k2 + pow((c2*w), 2)));
alpha = atan(-c2*w / k2);
}
//Static amplitude
staticamp = 0;
staticamp = Fo / keq;
Xp = (Fo / keq) / (sqrt(pow((1 - r*r), 2) + pow((2 * dampratio*r), 2)));
phi = atan((ceq*w) / (keq - m*w*w));
xp = Xp*sin(w*t - phi);
//Displacement Transmissiblity
Td = 0;
Td = (Fo / (Y*keq)) / (sqrt(pow((1 - r*r), 2) + pow((2 * dampratio*r), 2)));
//Frequanecy Response
H = 0;
H = 1 / (sqrt(pow((1 - r*r), 2) + pow((2 * dampratio*r), 2)));
//Force Transmissibilty to the bang
Ftbase = 0;
Ftbase = (Xp*sqrt(pow((k1 - keq*r*r), 2) + pow((c1*wn*r), r))) / (Y*k2);
//Force Transmissibilty to the wall
Ftwall = 0;
Ftwall = (Xp*sqrt((k1*k1) + pow((c1*r*wn), 2))) / (Y*k2);
if (cas == 5) {
cout << "\nCase: " << cas << endl << endl;
cout << "Wn: " << wn << endl;
cout << "Damping Ratio: " << dampratio << endl;
cout << "Amplitude: " << staticamp << endl;
cout << "Frequency Response: " << H << endl;
cout << "Phase Angle: " << phi << endl;
cout << "Displacement Transmissibilty: " << Td << endl;
cout << "Force Tranmissibility to the base: " << Ftbase << endl;
}
else {
cout << "\nCase: " << cas << endl << endl;
cout << "Wn: " << wn << endl;
cout << "Damping Ratio: " << dampratio << endl;
cout << "Amplitude: " << staticamp << endl;
cout << "Frequency Response: " << H << endl;
cout << "Phase Angle: " << phi << endl;
cout << "Displacement Transmissibilty: " << Td << endl;
cout << "Force Tranmissibility to the base: " << Ftbase << endl;
cout << "Force Transmissibilty to the wall: " << Ftwall << endl;
}
return 0;
}

How to print out the letters(characters) from a user inputed string?

using an array, i cannot seem to figure out how to print each letter on a separate line from a user inputted string.
Here is the code
#include <iostream>
#include <string>
using namespace std;
int main()
{
int x, z;
string fname, lname, name;
char name_array[20];
cout << "Please enter your name." << endl;
getline(cin, name);
x = name.length();
for(int i = 0; i < x; i++)
cout << name_array[i] << endl << endl;
z = name.find(' ', 0);
fname = name.substr(0, z);
lname = name.substr(z + 1, x);
cout << lname << ", " << fname << endl;
return 0;
}

Need advice on my quadratic formula functions

Okay The program compiles, but i am getting wrong output. Any help would be awesome. i do understand there are many other examples on this topic and i have looked at them all. None seemed to have help me clearly understand.
#include <iostream>
#include <cmath>
using namespace std;
double quadplus(float a, float b, float c);
double quadminus(float a, float b, float c);
int main()
{
double a, b, c;
double discriminant;
cout << "Please Enter variables for a, b, and c." << endl << endl;
cout << "Enter number for variable a." << endl << endl;
cin >> a;
cout << "Enter number for variable b." << endl << endl;
cin >> b;
cout << "Enter number for variable c." << endl << endl;
cin >> c;
discriminant = (b * b) - (4 * a * c);
if(discriminant == 0)
cout << quadplus(a,b,c) << endl;
else if(discriminant > 0)
cout << quadplus(a,b,c) << endl;
else if(discriminant < 0)
cout << quadminus(a,b,c) << quadplus(a,b,c) << endl;
return 0;
}
double quadplus(float a, float b, float c)
{
return ((-1 * b) + (sqrt(( b * b) - (4 * a * c))) / (2 * a));
}
double quadminus(float a, float b, float c)
{
return ((-1 * b) - (sqrt(( b * b) - (4 * a * c))) / ( 2 * a));
}
cout << quadplus << endl;
should probably be
cout << quadplus(a, b, c) << endl;
Also, if the discriminant is negative, your solutions will be
complex numbers of the form x + yi and your code doesn't properly account
for that. If discriminant != 0, you should probably print both roots.

To get pixel values of images

Below is the code which provides width,height and BGR values of 2 images.But the problem is until i close the first image i cant see the second image.What modifications to be made such that i can see both images at a time and get the all the pixel values.
1 . #include <cv.h>
#include<iostream>
#include <cxcore.h>
#include <highgui.h>
using namespace std;
int main(int argc, char** argv[])
{
int width,height;
int i=0,j=0,k=3,l=3;
IplImage *img1 = cvLoadImage("E:/images.jpg");
cvNamedWindow("Image1:",1);
cvShowImage("Image1:",img1);
cout << "Width:" << img1->width << endl;
cout << "Height:" << img1->height << endl;
CvScalar s;
s=cvGet2D(img1,i,j); // get the (i,j) pixel value
printf("B=%f, G=%f, R=%f\n",s.val[0],s.val[1],s.val[2]);
cvWaitKey();
cvDestroyWindow("Image1:");
IplImage *img2 = cvLoadImage("C:/Users/Public/Pictures/Sample Pictures/Tulips.jpg");
cvNamedWindow("Image2:",2);
cvShowImage("Image2:",img2);
cout << "Width:" << img2->width << endl;
cout << "Height:" << img2->height << endl;
s=cvGet2D(img2,k,l); // get the (k,l) pixel value
printf("B1=%f, G1=%f, R1=%f\n",s.val[0],s.val[1],s.val[2]);
cvWaitKey();
cvDestroyWindow("Image2:");
cvReleaseImage(&img1);
cvReleaseImage(&img2);
return 0;
}
problem is with cvWaitKey()
try:
IplImage *img1 = cvLoadImage("E:/images.jpg");
cvNamedWindow("Image1:",1);
cvShowImage("Image1:",img1);
cout << "Width:" << img1->width << endl;
cout << "Height:" << img1->height << endl;
CvScalar s;
s=cvGet2D(img1,i,j); // get the (i,j) pixel value
printf("B=%f, G=%f, R=%f\n",s.val[0],s.val[1],s.val[2]);
IplImage *img2 = cvLoadImage("C:/Users/Public/Pictures/Sample Pictures/Tulips.jpg");
cvNamedWindow("Image2:",2);
cvShowImage("Image2:",img2);
cout << "Width:" << img2->width << endl;
cout << "Height:" << img2->height << endl;
s=cvGet2D(img2,k,l); // get the (k,l) pixel value
printf("B1=%f, G1=%f, R1=%f\n",s.val[0],s.val[1],s.val[2]);
cvWaitKey();
cvDestroyWindow("Image1:");
cvDestroyWindow("Image2:");
cvReleaseImage(&img1);
cvReleaseImage(&img2);

Resources