Hellou guys, I am a beginner with self learner of C++.
today I tried to make a simple calculator but the debugger keeps on showing me the same error on and on. Unitianalized variable used "X" ; Unitianalized variable used "Z"
Here is the code :
#include <iostream>
using namespace std;
int main()
{
float x, z, a;
a = x + z;
cout << "Welcome to the calculator" << endl;
cout << "State the first number " << endl;
cin >> x ;
cout << "State the second number " << endl;
cin >> z ;
cout << "If you wanted to time number" << x << "by this number" << z << "The result would be : " << a << endl;
system("pause");
return 0;
}
You should initialize your variables and calculate a after you read x and z. Take a look at this: https://www.learncpp.com/cpp-programming/eight-c-programming-mistakes-the-compiler-wont-catch/
#include <iostream>
using namespace std;
int main()
{
float x = 0.0f, z = 0.0f, a = 0.0f;
cout << "Welcome to the calculator" << endl;
cout << "State the first number " << endl;
cin >> x ;
cout << "State the second number " << endl;
cin >> z ;
a = x + z;
cout << "If you wanted to time number" << x << "by this number" << z << "The result would be : " << a << endl;
system("pause");
return 0;
}
The order in which you do things matters.
int x = 5, z = 2;
int a = x + z; // a is 7
z = 5; // a is still 7
a = x + z; // now a is updated to 10
So in your code when you do a = x + z; both x and z are uninitialized. It's undefined behavior to use uninitialized variables.
To fix it, move the a = x + z; to after you have input values to x and z.
Related
I developed a program to tabulate a given interval [a;b] with a step of c, and also find its largest and smallest value on this interval. I'm not sure if I have it right, so I wanted some advice. This code has a picture with a task condition.enter image description here
In the process of solving the given problem, apply the loop operator with a prerequisite. In the process of implementing the given task, assume that the argument of the function is identified as x, and the identifier of the variable responsible for the value of the function is y.
#include <iostream>
#include <math.h>
#include <clocale>
#define _USE_MATH_DEFINES
#include <iomanip>
#include<climits>
using namespace std;
int main(){
setlocale(LC_CTYPE, "");
double x, y, a, b, c;
double max, min, max_y, min_y;
max = -INT_MAX;
min = INT_MAX;
cout << "\n a:";
cin >> a;
cout << "\n b:";
cin >> b;
cout << "\n c:";
cin >> c;
cout << "\n a = " << a;
cout << " b = " << b;
cout << " c = " << c;
y = a;
while(y <= b){
if(y > 1) x = sin(sqrt(y + log(y)));
if((0 <= y) && (y <= 1)) x = M_PI + pow(cos(y + 1.2), 2);
if(y < 0) x = y * log10(pow(y,2) +2) + M_PI;
if(x > max){
max = x;
max_y = y;
}
if(x < min){
min = x;
min_y = y;
}
cout << "\n x = " << setw(8) << x << " y = " << setw(8) << y;
y += c;
}
cout << "\n The largest value in the given interval is" << max << " at y = " << max_y << "\n";
cout << "\n The smallest value in the given interval is" << min << " at y = " << min_y << "\n";
return 0;
}
When entering the values a,b,c into the console, our program should tabulate the function (output to the console) and find the smallest and largest value in the interval. I have the program working, but I'm not sure if it's correct. I would like to hear some advice
Added small updates:
Looks good overal
#include <iostream>
#include <cmath> //math.h == cmath
#include <clocale>
#define _USE_MATH_DEFINES
#include <iomanip>
#include<climits>
using namespace std;
void tabulate(double from, double to, double step)
{
double max, min, max_y, min_y, x = 0;
max = from; //they all start at 'from' anyway, its always best to set the max and min to first element of the array you're going through. They get overwritten anyway.
min = from;
for(double y = from; y < to; y += step)
{
if(y > 1) x = sin(sqrt(y + log(y)));
if((0 <= y) && (y <= 1)) x = M_PI + pow(cos(y + 1.2), 2);
if(y < 0) x = y * log10(pow(y,2) +2) + M_PI;
if(x > max){
max = x;
max_y = y;
}
if(x < min){
min = x;
min_y = y;
}
cout << " x = " << setw(8) << x << " y = " << setw(8) << y << endl;
}
cout << "The largest value in the given interval is " << max << " at y = " << max_y << endl;
cout << "The smallest value in the given interval is " << min << " at y = " << min_y << endl;
}
int main(){
setlocale(LC_CTYPE, "");
double a,b,c;
cout << "a: ";
cin >> a;
cout << "b: ";
cin >> b;
cout << "c: ";
cin >> c;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
tabulate(a,b,c);
return 0;
}
Just in case the title wasn't descriptive enough, I wrote a basic calculator as a beginner and after random stress-testing and experimenting trying to find bugs, I found something that results in the following as an example.
Would you like to add, subtract, multiply, divide, or use powers? (A,S,M,D,P)
D
What integer would you like to start with?
48834343
What would you like to divide 48834343 by?
34923499234
48834343 divided by 3.49235e+10 is equal to 0.00139832
The code is somehow shifting the decimal over or just multiplying it by a random multiple of 10. I have tried using different variable types which seemed to fix it a little bit but when you start increasing the character count of the number(s), the more it seems to break with most variable types. I'm probably missing something since I'm a beginner, so don't hold anything back in any explanation as far as criticism goes, but please ELI5.
Enough chitchat, here's the code
#include<iostream>
#include<math.h>
using namespace std;
void add() {
cout << "What integer would you like to start with?\n";
int x;
cin >> x;
cout << "What would you like to add to " << x << "?\n";
int y;
cin >> y;
cout << x << " plus " << y << " is equal to " << x + y;
}
void subt() {
cout << "What integer would you like to start with?\n";
int x;
cin >> x;
cout << "What would you like to subtract from " << x << "?\n";
int y;
cin >> y;
cout << x << " minus " << y << " is equal to " << x - y;
}
void mult() {
cout << "What integer would you like to start with?\n";
double x;
cin >> x;
cout << "What would you like to multiply " << x << " by?\n";
double y;
cin >> y;
cout << x << " times " << y << " is equal to " << x * y;
}
void divd() {
cout << "What integer would you like to start with?\n";
int x;
cin >> x;
cout << "What would you like to divide " << x << " by?\n";
float y;
cin >> y;
cout << x << " divided by " << y << " is equal to " << x / y;
}
void power() {
cout << "What integer would you like to start with?\n";
long double x;
cin >> x;
cout << x << " to the power of what?\n";
long double y;
cin >> y;
cout << x << " to the power of " << y << " is equal to " << pow(x, y);
}
int main()
{
cout << "Would you like to add, subtract, multiply, divide, or use powers? (A,S,M,D,P)";
string ans;
cin >> ans;
//user responds with the first letter of the operation they want
if (ans == "A") {
add();
return 0;
}
else if (ans == "S") {
subt();
return 0;
}
else if (ans == "M") {
mult();
return 0;
}
else if (ans == "D") {
divd();
return 0;
}
else if (ans == "P") {
power();
return 0;
}
else cout << "Error, try again with one of the above letters";
return 1;
}
int main() {
bool x,y,z;
cin >> x >> y >> z;
bool value = false; // change this line
cout << x << " XOR " << y << " XOR " << z << " = " << value << endl;
}
Please help me fix "change this line."
its simple
bool value = x^y^z;
The code I am currently working on is a random walker. The code represents a person taking a step in any random direction (up,down,left,right), then the new location is printed out respectively. In the beginning the user is prompted to enter any amount of steps or how many times the loop should be iterated. The goal of the code is to calculate the squared distance between (0,0)initial and (x,y)final. The distance should be equal to (xx)+(yy) because the initial position that would normally be subtracted is (0,0). The issue or semantic issue I am running into is with the distance calculation. The calculation is not always using the correct x or y value. For example if the final location was (0,-4), somehow x = -1, therefore the distance equals 17 instead of 16. This first example is in image 1. Image 2 is another run for the code. Any help or tips would be greatly appreciated, here is the code:
#include <iostream>
#include <stdlib.h>
#include <ctime>
#include <cmath>
using namespace std;
int main(){
int N;
cout << "Please enter N amount of steps, and for NetBeans users press
'enter' twice." << endl;
cin >> N;
cout << "% RandomWalker " << N << endl;;
int r;
srand( time(0));
int x = 0;
int y = 0;
for(int i = 0; i <= N; i++) {
cout << "(" << x << ", " << y << ")" << endl;
r=rand()%4;
if (r == 0 )
x++;
else if (r == 1 )
x--;
else if (r == 2 )
y++;
else if (r == 3 )
y--;
}
int d = (x*x)+(y*y);
cout << "the distance equals: " << d << endl;
cout << endl;
cout << "x equals before: "<< x << endl;
x = (pow(x,2));
cout << "x equals after squaring: "<< x << endl;
cout << endl;
cout <<"y equals before: " << y << endl;
y = (pow(y,2));
int sum = x + y;
cout <<"y equals after squaring: " << y << endl;
cout << endl;
cout << "x+y after squaring equals: " << sum << endl;
}
for(int i = 0; i <= N; i++)
Since you are starting from 0, the condition should be i < N.
Next issue, is int d = (x*x)+(y*y); The distance formula is
So the initialization should be
int d = sqrt((x * x) + (y * y));
Also what is the point of squaring the x and y values at the end?
You are printing the position BEFORE it is changed based on the value of r. So the last value that's printed out is not the actual final value of x and y, but the one of one step earlier. That's why you're getting unexpected distance.
Sorry for the delayed response, I figured out what I had to do. The object of the assignment was to print out each location and find the squared distance at the end of the loop. Here was the solution for anyone interested.
#include <iostream>
#include <stdlib.h>
#include <ctime>
#include <cmath>
using namespace std;
int main(){
int N;
cout << "Please enter N amount of steps, and for NetBeans users press
'enter' twice." << endl;
cin >> N;
cout << "% RandomWalker " << N << endl;;
int r;
srand( time(0));
int x = 0;
int y = 0;
cout << "(" << x << ", " << y << ")" << endl;
for(int i = 1; i <= N; i++) {
r=rand()%4;
if (r == 0 )
x++;
else if (r == 1 )
x--;
else if (r == 2 )
y++;
else if (r == 3 )
y--;
cout << "(" << x << ", " << y << ")" << endl;
}
x = (pow(x,2));
y = (pow(y,2));
int squaredDistance = x + y;
cout << "The squared distance is " << squaredDistance << endl;
}
So I am completely new to C++ and attempting to use Xcode to write this code that analyzes two integers. I keep getting an error on the "if" line that says "use of undeclared identifier x, and "use of undeclared identifier y" I also get an error on the "else" line that says "Expected expression". What do I need to do/change to make this program work? Help.
#include <iostream>
using namespace std;
void swap (int &x, int &y);
int main()
{
cout << "Please enter two integers:/n";
cout << "First integer ==> " " >> x >> ";
cout << "Second integer ==> " " >> y >> ";
cout << endl;
if ( &x < &y )
cout << "Your integers are in the correct order:/n";
cout << " << x << " " << y << ";
else void swap(int &x, int &y);
cout << "Your integers have been swapped:/n";
cout << " << x << " " << y << ";
}
The variables x and y do not exist in the scope of the function main.
You just declared those variables in the function declaration swap, and happen to name those variables x and y. That's what these errors mean. The variables do not exist in the current scope.
use of undeclared identifier x
use of undeclared identifier y
You also do not ever define that function, so you are likely name-shadowing the std version of swap.
You need to declare and assign x and y inside a function (or outside) that refers to the values:
for example
int main()
{
int x = 5, y = 10;
if ( &x < &y )
cout << "Your integers are in the correct order:/n";
cout << " << x << " " << y << ";
else void swap(int &x, int &y);
//...
}
edit:
#include <iostream>
using namespace std;
void swap (int &x, int &y);
int main()
{
int x = 0, y = 0;
cout << "Please enter two integers:/n";
cout << "First integer ==> " " >> x >> ";
cout << "Second integer ==> " " >> y >> ";
cout << endl;
if ( x < y )
{
cout << "Your integers are in the correct order:/n";
cout << " << x << " " << y << ";
}
else
{
swap(int &x, int &y);
cout << "Your integers have been swapped:/n";
cout << " << x << " " << y << ";
}
return 0;
}
void swap (int &x, int &y)
{
// define here
}