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)
Related
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.
I have written a code in c++ to check the variable increment (Screenshot added). In line 13 when I use "++x" in printing function to print the value of x and y. The value I'm getting is not equal but the memory address is same.
In line 17 I've incremented y as ++y and I got my expected equal ans
(Screenshot added) My Code Screenshot.
What is the reason for not getting the unexpected ans in line 13?
My Code: https://gist.github.com/mefahimrahman/7fb0f45ae1f45caeb97d5aaeb39c4833
#include<bits/stdc++.h>
using namespace std;
int main()
{
int x = 7, &y = x;
cout << "Whithout Increment: ";
cout << &x << " " << &y << endl;
cout << x << " " << y << endl;
--x;
cout << "After x Increment: ";
cout << &x << " " << &y << endl;
cout << ++x << " " << y << endl;
y++; cout << "After y Increment: ";
cout << &x << " " << &y << endl;
cout << x << " " << ++y << endl;
}
You are assuming that in
cout << ++x << " " << y << endl;
++x will be evaluated before the value of y is accessed. In other words you are assuming that your output expression is evaluated left to right. But this is not necessarily the case. Change your code to this
++x;
cout << x << " " << y << endl;
and you will get the output you expect.
Also newbies sometimes assume that ++x means the x will be incremented before anything else. But again this is not true, ++x just means that x will be incremented before the value of x is taken, not before anything else.
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);}
^
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
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i want to know how to turn this if else statement to a switch statement.this is a c++ program to out put the integers in the right order. i can't figure out a way.please help.thank you in advance.
int x, y, z;
cout << "please enter three integers:";
cin >> x >> y >> z;
if (x <= y && y <= z)
cout << x << " " << y << " " << z << endl;
else if (x <= z && z <= y)
cout << x << " " << z << " " << y << endl;
else if (y <= z && z <= x)
cout << y << " " << z << " " << x << endl;
else if (y <= x && x <= z)
cout << y << " " << x << " " << z << endl;
else if (z <= x && x <= y)
cout << z << " " << x << " " << y << endl;
else
cout << z << " " << y << " " << x << endl;
if (x <= y) {
if (z <= x) {
cout << z << " " << x << " " << y << endl;
} else {
if (z <= y) {
cout << x << " " << z << " " << y << endl;
} else {
cout << x << " " << y << " " << z << endl;
}
}
} else {
if (z >= x) {
cout << y << " " << x << " " << z << endl;
} else {
if (y >= z) {
cout << z << " " << y << " " << x << endl;
} else {
cout << y << " " << z << " " << x << endl;
}
}
}
That is not a good way to write a program. What if you need 4 integers?
One way to do it would be to use a list like std::vector and sort it.
vector<int> numbers;
int number = 0;
while (numbers.size() < 3 && cin >> number)
numbers.push_back(number);
sort(cbegin(numbers), cend(numbers)); // sorts ascending by default
for (auto number : numbers)
cout << number << " ";
cout << endl;
You can also use std::multiset which sorts its items automatically as they are inserted.
Instead of this approach, why don't you store the input in an array and try any of the sorting alogrithms to sort your input?
The answer in your particular case is you can't,
Your if statements have particular conditions, which,
wouldn't really work inside a switch.
An example of if/else if/else statements that can be
translated into a switch would be:
if(i == 1) {
// code here
} else if(i == 2) {
// code here
} else if(i == 3) {
// code here
} else {
// code here
}
which would translate to:
switch(i) {
case 1:
//code here
break;
case 2:
//code here
break;
case 3:
//code here
break;
default:
//code here
break;
}
Hope that helps a bit to understand a little more about switch statements.
If the challenge is that you can't sort the numbers, and the limit is 3 integers, then there are better ways to accomplish what you're doing.
#include <iostream>
using namespace std;
int main()
{
int combo[6][3] = {{0,1,2},{0,2,1},{1,0,2},{1,2,0},{2,0,1},{2,1,0}};
int values[3];
cin >> values[0] >> values[1] >> values[2];
for (int i = 0; i < 6; ++i )
{
if ( values[combo[i][0]] <= values[combo[i][1]] &&
values[combo[i][1]] <= values[combo[i][2]] )
{
cout << values[combo[i][0]] << " " << values[combo[i][1]] <<
" " << values[combo[i][2]];
break;
}
}
}
The combo array holds all of the combinations that can occur with 3 slots. Also note that the input is an array. Even if you didn't use the loop, the maximum if() statements you would need is 6.
However you should mention up front that this is a challenge, and what the restrictions are. Otherwise, please look at the other answers concerning storing and sorting these numbers.
You only need three conditional swaps to sort a list of three elements:
int x, y, z;
cout << "please enter three integers:";
cin >> x >> y >> z;
if (y < x) swap(y, x);
if (z < y) swap(z, y);
if (y < x) swap(y, x);
cout << x << " " << y << " " << z << endl;
Note that this approach doesn't scale. For 10 numbers, you would already need 45 conditional swaps.