Use of undeclared identifier and expected expression error. Xcode - c++

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
}

Related

Easy calculator

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.

Compare getline() to predetermined variable in an 'If' statement

First off, I just started learning C++, so please forgive me if I titled this incorrectly, or I don't know what something means. If you do answer this question, please don't use all those crazy vocabulary words that I wouldn't know. Thanks :).
I am currently (trying) to make a calculator, that give you a choice to either add subtract, multiply, or divide. I know this doesn't seem like something that would be very helpful, but of the things I have done, I have learned a lot from it.
Here is the code that I have right now:
#include <iostream>
#include <string>
using namespace std;
int add()
{
int x;
int y;
int sum;
cout << "Enter the value for x: ";
cin >> x;
cout << "Enter the value for y: ";
cin >> y;
sum = x + y;
cout << x << " + " << y << " = " << sum << endl;
}
int subtract()
{
int x;
int y;
int difference;
cout << "Enter the value for x: ";
cin >> x;
cout << "Enter the value for y: ";
cin >> y;
difference = x - y;
cout << x << " - " << y << " = " << difference << endl;
}
int multiply()
{
int x;
int y;
int product;
cout << "Enter the value for x: ";
cin >> x;
cout << "Enter the value for y: ";
cin >> y;
product = x * y;
cout << x << " * " << y << " = " << product << endl;
}
int divide()
{
int x;
int y;
int quotient;
cout << "Enter the value for x: ";
cin >> x;
cout << "Enter the value for y: ";
cin >> y;
quotient = x / y;
cout << x << " / " << y << " = " << quotient << endl;
}
int main()
{
int Add, Subtract, Multiply, Divide;
string str;
cout << "Add, Subtract, Multiply, Divide?: ";
getline(cin, str);
if (str == (cin, add));
cout << "Starting process " << str << "..." << endl;
add();
getline(cin, str);
if (str == (cin, "subtract"));
cout << "Starting process " << str << "..." << endl;
subtract();
}
The Issue I am having, is in the main() part. I want the program to read the user input via getline and then have it compare it to a predetermined variable. Then, I tried to make an if statement, that, if the user input was for instance "add", then the if statement reads that, and then runs my add function.
I got as far as the getline part, but as you can see, the if statement does nothing. I was thinking it might be something like:
if (str == "add")
cout << "Starting Process" << str << "..." << endl;
add();
All that got me was some errors. I'm not sure how to construct the if statement, and any help would be greatly appreciated.
Thanks,
Nick
So, I did some research, and I got this to finally work. I apologize if this caused you any inconvenience. I can see though, that this is a very active and nice community. I will make sure to come back to it as a "first line of defence". Here is my final code:
#include <iostream>
using namespace std;
void add()
{
int x;
int y;
int sum;
cout << "Enter the value for x: ";
cin >> x;
cout << "Enter the value for y: ";
cin >> y;
sum = x + y;
cout << x << " + " << y << " = " << sum << endl;
}
void subtract()
{
int x;
int y;
int difference;
cout << "Enter the value for x: ";
cin >> x;
cout << "Enter the value for y: ";
cin >> y;
difference = x - y;
cout << x << " - " << y << " = " << difference << endl;
}
void multiply()
{
int x;
int y;
int product;
cout << "Enter the value for x: ";
cin >> x;
cout << "Enter the value for y: ";
cin >> y;
product = x * y;
cout << x << " * " << y << " = " << product << endl;
}
void divide()
{
int x;
int y;
int quotient;
cout << "Enter the value for x: ";
cin >> x;
cout << "Enter the value for y: ";
cin >> y;
quotient = x / y;
cout << x << " / " << y << " = " << quotient << endl;
}
int main()
{
int Add, Subtract, Multiply, Divide;
string str;
cout << "Add, Subtract, Multiply, Divide? (Letters are case sensitive): ";
getline(cin, str);
if (str == "Add")
{
cout << "Starting Process " << str << "..." << endl;
add();
}
//getline(cin, str);
if (str == "Subtract")
{
cout << "Starting process " << str << "..." << endl;
subtract();
}
//getline(cin, str);
if (str == "Multiply")
{
cout << "Starting process " << str << "..." << endl;
multiply();
}
//getline(cin, str);
if (str == "Divide")
{
cout << "Starting process " << str << "..." << endl;
divide();
}
}

" invalid operands of types 'int' and 'int* const'" error from function, from book

I am going through the book "Beginning C++ Through Game Programming". I have typed this supplied script perfectly (I even pasted over it with the script supplied from online download- and used undo/redo to compare every character- to find no difference- except line endings and actually capitalizing the first character of function names). Despite no change, the supplied script compiles perfectly fine, but mine does not (unless I copy/paste supplied script). I am given the error in the second function: GoodSwap()
#include<iostream>
using namespace std;
void BadSwap(int x, int y);
void GoodSwap(int* const pX, int* const pY);
int main(){
int myScore = 150;
int yourScore = 1000;
cout << "Original Values" << endl;
cout << "myScore : " << myScore << endl;
cout << "yourScore: " << yourScore << endl;
cout << "Calling BadSwap()" << endl;
BadSwap(myScore, yourScore);
cout << "myScore : " << myScore << endl;
cout << "yourScore: " << yourScore << endl;
cout << "Calling GoodSwap()" << endl;
GoodSwap(&myScore, &yourScore);
cout << "myScore : " << myScore << endl;
cout << "yourScore: " << yourScore << endl;
return 0;
}
void BadSwap(int x, int y){
int temp = x;
x = y;
y = temp;
}
void GoodSwap(int* const pX, int* const pY){
int temp = *pX
*pX = *pY;
*pY = temp;
}
You missed a ; at first line of GoodSwap().
int temp = *pX // There should be a `;`
After this change, your program can compile in VS2015.

Copy constructor issue [duplicate]

This question already has answers here:
C++: Argument Passing "passed by reference"
(4 answers)
Why is the copy constructor called when we pass an object as an argument by value to a method?
(3 answers)
Closed 6 years ago.
The program should resolve grade 1 equations in this specific manner. I had to use output messages for the constructors and destructors for better understanding of the code.
#include "stdafx.h"
#include <iostream>
using namespace std;
class Ec {
public: float a, b; //equation's parameters
public:
Ec() {float x, y; cout <<"a= "; cin >> x; cout << "b= ";
cin >> y; a = x; b = y; cout << "void constr\n";};
Ec(float x, float y) { a = x; b = y; cout << "param constr\n"; }
~Ec() { cout << "destr\n"; }
Ec(Ec &z) { a = z.a; b = z.b; cout << "cpy constr\n"; }
friend float half1(Ec); //function to return a/2
friend float half2(Ec); //function to return b/2
friend float sol1(Ec); //function to return the solution for the standard eq
friend float sol2(Ec); //function to return the sol for the /2 param eq
};
float half1(Ec ec1) { return (ec1.a / 2);}
float half2(Ec ec1) { return (ec1.b / 2); }
float sol1(Ec ec1) { float x; return x = -ec1.b / ec1.a; }
float sol2(Ec ec1) { float x2; return x2 = -half2(ec1) / half1(ec1); }
int main()
{
int x, y;
cout << "a= ";
cin >> x;
cout << "b= ";
cin >> y;
Ec ec1;
Ec ec2(x, y);
Ec ec3 = ec1;
//the couts display for ex:" ec 2x+1=0 has sol -0.5"
cout << "ec " << ec1.a << "x+ " << ec1.b << "=0 has sol " << sol1(ec1) << endl;
cout << "ec " << ec2.a << "x+ " << ec2.b << "=0 has sol " << sol1(ec2) << endl;
cout << "ec " << ec3.a << "x+ " << ec3.b << "=0 has sol " << sol1(ec3) << endl;
cout << "ec halved " << half1(ec1) << "x+ " << half2(ec1) << "=0 has sol " << sol2(ec1) << endl;
cout << "ec halved " << half1(ec2) << "x+ " << half2(ec2) << "=0 has sol " << sol2(ec2) << endl;
cout << "ec halved " << half1(ec3) << "x+ " << half2(ec3) << "=0 has sol " << sol2(ec3) << endl;
}
return 0;
}
Now, I have troubles understanding why after the first cpy constructor(for ec3) another cpy constructor is called then a destructor. What is it doing?
Your functions take Ec objects by value
float half1(Ec ec1) { return (ec1.a / 2);}
^
These will make function local copies that are then destroyed at the end of each function.
If you want to avoid making these copies, pass the arguments by const reference
float half1(Ec const& ec1) { return (ec1.a / 2);}
^

Error: control reaches end of non-void function? [duplicate]

This question already has answers here:
Why does flowing off the end of a non-void function without returning a value not produce a compiler error?
(11 answers)
Closed 6 months ago.
This is my assignment : " Write a function that finds the larger of two integers input (in the main program) but allows you to change the value of the integers in the function using pass by reference." I been trying to find out whats wrong with my code but I cant find out what it is. Someone please help me !! this assignment is due tonight!!
Here is my code as of right now :
#include <iostream>
using namespace std;
int change(int& x, int& y)
{
int temp=0;
cout << "Function(before change): " << x << " " << y << endl;
temp = x;
x = y;
y = temp;
cout << "Function(after change): " << x << " " << y << endl;
}
int main()
{
int x,y;
cout << " Please Enter your first number: ";
cin >> x;
cout << " Please Enter your second number: ";
cin >> y;
if (x > y)
cout << x << " is greater than " << y << endl;
if (x < y)
cout << y << " is greater than " << x << endl;
if (x == y)
cout << x << " is equal to " <<y << endl;
cout << "Main (before change): " << x << " " << y << endl;
change(x, y);
cout << "Main (after change): " << x << " " << y << endl;
system("Pause");
return 0;
}
change is declared as returning an int, but it never returns anything. It doesn't look like your function should return anything, so just declare it as void:
void change(int& x, int& y)