There are 2 errors expression must be a modifiable lvalue (line 12) and expected a statement (line 25). I am new to c++ and i only know scratch and qbasic. I have been sitting in front of this error for almost 2 hours now.
Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int d;
cout << "Do you want Addition or Subtraction? (a/s)";
cin >> d;
if ('d' = "s");
{
int a, b;
cout << "Enter any number: ";
cin >> a;
cout << "Enter another number: ";
cin >> b;
int c = a + b;
cout << "The sum is ";
cout << c;
return 1;
}else{
int e, f;
cout << "Enter any number: ";
cin >> e;
cout << "Enter another number: ";
cin >> f;
int g = e + f;
cout << "The sum is ";
cout << g;
return 0;
}
}
Change
if ('d' = "s");
{
to
if (d == 's')
{
This fixes 4 problems in your code
the variable name is d & not 'd' (remove the single quotes around d in the if check. d is the variable int d. 'd' is the character d.
Conditional Check should be == & not =. = is assignment. == is conditional check. This is the cause for the l-value error (https://softwareengineering.stackexchange.com/questions/155665/what-are-the-key-terms-rvalue-and-lvalue)
The RHS of the conditional check should be 's' and not "s". Something between single quotes is a character. Something between double quotes is a string.
There should be no semicolon at the end of the if check. Remove it.
Also change int d to char d
Related
when user input a character and press enters to take next input, output window asks to press any key and exit, please help me out to get remaining code working,
Here is the code
char r, R;
char c;
int channels;
int resedential()
{
// float channelscost = costperchannel*channels;
// float Bill = channelscost+processingfee+basiccost;
// cout << "Dear Customer Here is Your Bill" <<" "<<Bill<<endl;
}
int main()
{
cout << " Enter Customer Type" << " " << endl;
cin >> c;
if( c == r || R )
{
cout << " Enter Customer Type" << " " << endl;
cin >> c;
}
}
One thing I recognise, is that your if statement doesn't do what you think it does. Right now you aren't checking if c is equal to R.
Fixed Code:
if(c == r || c == R)
{
cout << "Enter Customer Type " << endl;
cin >> c;
}
Also check that there are actual values in r and R. So it doesn't compare to a constant (char(0)) – ASCII code NUL, resulting from default initialization of file-scoped variables to zero.
Example:
char r = 'A';
char R = 'B';
Hello this is my first program with a do-while loop and its taken me a little while to get it down. I need to have the user enter 2 numbers, and raise the first number to the second number. I have finally got the coding to ask if "they would like to raise another number by a power?" and when they say yes and enter 2 new numbers the total adds the total from the first 2 numbers entered with the second set of numbers and so on. Can someone help me out with this problem? Here is the coding and a picture to help y'all out!
#include <iostream>
using namespace std;
int main()
{
int num;
int pow;
int p;
int power = 1;
char yesno = 'y' || 'Y';
do
{
cout << "Enter a number: ";
cin >> num; "\n";
cout << "Enter the power to raise: ";
cin >> pow; "\n";
for (p = 1; p <= pow; p++)
{
power = power * num;
}
cout << "The total is: " << power << endl;
cout << "\n\n";
cout << "Would you like to raise another number by a power? [Y/N]";
cin >> yesno;
} while (yesno != true);
}
The problem of the ever-increasing answer is that power is not being reset inside the do-while loop, so the last value is being carried forward into the next loop. You need reset it at the top of the loop.
Another problem with the code is that the exit condition would never occur.
Try this instead:
int main()
{
int num;
int pow;
int p;
int power;
char yesno;
do
{
power = 1; // <<<<<< reset power here
cout << "Enter a number: ";
cin >> num; "\n";
cout << "Enter the power to raise: ";
cin >> pow; "\n";
for (p = 1; p <= pow; p++)
{
power = power * num;
}
cout << "The total is: " << power << endl;
cout << "\n\n";
cout << "Would you like to raise another number by a power? [Y/N]";
cin >> yesno;
} while (yesno == 'y' || yesno == 'Y'); // <<<<< test for 'yes' response
}
When you reach line } while (yesno != true); and loop back to do {, the variable power still holds the previous num^pow. You will need to assign power = 1 after do {.
#include <iostream>
// you also need
#include <cmath> // for pow()
using namespace std;
int main()
{
// int num; Declare variables where they're used. As locally as possible.
// int pow;
// int p;
// int power = 1;
// char yesno = 'y' || 'Y'; I don't know what you were trying to do here
// the expression 'y' || 'Y' will always be true
// and evaluate to some value different from null
// wich will be assigne to yesno. But with no con-
char yesno; // sequences since it later gets overwritten by
do // cin >> yesno; There is no need to initialize
{ // this variable.
cout << "Enter a number: ";
int num;
cin >> num; "\n"; // the statement "\n"; has no effect.
cout << "Enter the power to raise: ";
int pow;
cin >> pow; "\n"; // again. no effect.
// for (p = 1; p <= pow; p++) as user4581301 has pointed out in the
// comments it is more ... natural in C
// to loop from 0 to < max:
int power = 1; // now its time to declare and define power ;)
for(int p = 0; p < pow; ++p) // notice that you can declare variables
{ // in the init-statement of a for-loop
// power = power * num; shorter:
power *= num;
}
cout << "The total is: " << power << /* endl; + 2 x '\n' gives: */ << "\n\n\n";
// cout << "\n\n";
cout << "Would you like to raise another number by a power? [Y/N]";
cin >> yesno;
// } while (yesno != true); that condition will most likely always be true
// since the user would have a hard time to input
// a '\0' character, which would evaluate to false
// better:
} while(yesno == 'y' || yesno == 'Y' );
}
done.
Without clutter:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
char yesno;
do {
cout << "Enter a number: ";
int num;
cin >> num;
cout << "Enter the power to raise: ";
int pow;
cin >> pow;
int power = 1;
for(int p = 0; p < pow; ++p)
power *= num;
cout << "The total is: " << power << "\n\n\n";
cout << "Would you like to raise another number by a power? [Y/N]";
cin >> yesno;
} while(yesno == 'y' || yesno == 'Y' );
}
I want to write the programm, that takes two numbers m and n as input and gives n-th digit of m number. Example m=1358 n=2 Output: 5
#include <iostream>
#include <string>
using namespace std;
int main(){
while(true){
int m = 0,n = 10;
char check;
while(true){
cout << "Enter please number m and which digit you want to select";
cin >> m >> n;
string m_new = to_string(m);
if(m_new.length() > n)
break;
else
cout << "n must be less than or equal to m";
}
cout << "The position" << n << "Of integer" << m << "is:" << m_new.substr(n,1);
cout << "Do you want to try again?(y/n)\n";
cin >> check;
while(check != 'y'&& check != 'n'){
cout <<"Please enter y or n\n";
cin >> check;
}
if(check == 'n'){
break;
}
}
return 0;
}
But i got an error: 'm_new' was not declared in this scope. Why do I get this error and how to fix it?
The m_new variable is local to nested while loop and can't be used outside of its scope. Scope represented by braces {} determines visibility:
int main() {
// can't be used here
while (true) {
// can't be used here
while (true) {
string m_new = to_string(m);
// can only be used here
}
// can't be used here
while (check != 'y'&& check != 'n') {
// can't be used here
}
// can't be used here
}
// can't be used here
}
Rethink the design to use only one while loop:
int main(){
char check = 'y';
while (std::cin && choice == 'y') {
int m = 0, n = 10;
std::cout << "Enter please number m and which digit you want to select";
std::cin >> m >> n;
string m_new = to_string(m);
// the rest of your code
std::cout << "Please enter y or n\n";
std::cin >> check;
}
}
Now the m_new variable is visible to everything inside the while loop.
m_new is declared inside the while loop. Anything declared inside a {...} block will only exist inside that block. The final use of it:
cout << "The position" << n << "Of integer" << m << "is:" << m_new.substr(n,1);
is outside the block, and therefore the variable no longer exists.
The variable "m_new" was just in the "while(true)" scope, and when your're asking this variable out of loop, it throws a compile-time error.
while(true){
...
string m_new = to_string(m);
...
}
...
cout << "The position" << n << "Of integer" << m << "is:" << m_new.substr(n,1);
^
...
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.
So far my only problem with this code is that C won't initialize. I know that if I make degree_type == "C" it won't compile because I can't turn an int into a character. What's exactly wrong with this code?
#include <iostream>
using namespace std;
int main()
{
char C;
double degree;
int degree_type;
cout << "What's the Degree type?: ";
cin >> degree_type;
if (degree_type == C)
{
cout << "What's the Temperature:? ";
cin >> degree;
cout << "Your Degrees in Celsius is, " << 9 / 5 * degree + 32 << " degrees fahrenheit." << endl;
}
else
{
cout << "What's the Temperature:? ";
cin >> degree;
cout << "Your Degrees in Fahrenhait is, " << (degree - 32) * 5 / 9 << " degrees Celsius." << endl;
}
return 0;
}
You are (or were, before you changed your question) using cin to read a character. When you read one character, the next character (the Enter keypress) remains in the input buffer waiting to be read. The next time you read from cin (to get the temperature), it will immediately see the Enter keypress from the previous input and not let you type anything.
Use getline instead:
std::string str;
std::getline(std::cin, str);
degree_type = str.at(0);
Once you have done that, the test degree_type = C does not do what you think it does for two reasons:
The single equals = is assignment. For comparison, use ==.
The C is the name of a variable. For the character C, use 'C'.