lvalue required as increment operand error when using #define - c++

Whats the problem with the code below, and why I'm getting this error?
#include <iostream>
#define A 1
using namespace std;
int main()
{
cout <<A++;
return 0;
}

#define A 1 doesn't make a variable called A.
It tells your computer to replace all utterances of A with 1 before compiling.
So, your program is actually:
#include <iostream>
using namespace std;
int main()
{
cout <<1++;
return 0;
}
And you cannot increment the literal 1.
You may read more about preprocessor directives in your C++ book.

#define A 1
A is not valid c++ left value, so gave us "lvalue required as increment operand"
int A;
is valid c++ left value and will work, also of other simple number type float, unsigned char etc

Related

how do i fix this no matching function for call to 'stoi(int&)'|

i keep getting this error. i know this is a c++ 11 function but it still isnt working with code blocks c++ compiler. am i using this function correctly of is it a problem with the codeblocks compiler. i tried changing the compiler. using the "have g++ follow the c++11 iso standard" i still keep getting this error. or getting the "stoi() does not exist in the current scope" error
#include <iostream>
#include <string>
using namespace std;
int main()
{
int test = 34;
cout << stoi(test);
}
stoi means "String To Int". It will read an int from a std::string (or std::wstring). See also the reference.
You were probably looking for the reverse std::to_string (reference). But you don't need either, there is no need to convert to string before printing:
#include <iostream>
int main()
{
int test = 34;
std::cout << test;
}
stoi means string to int. So it takes a string as an input.
This should work:
string test = "34"; cout << stoi(test);

Reversing a string with C++

#include <iostream>
#include <string>
using namespace std;
int main()
{
string s="PIZZA";
int le=s.length();
for(int i=le-1;i=0;i--){
cout<<s[i];
}
}
What is the error here? I am not getting any output.
You 'd mean i >= 0 in the for loop.
Otherwise you never enter it. i = 0 results to 0 which results to false.
Please do learn how to use the debugger, you will solve most of your problems with it. Unrelated: Don't use using namespace std globally, avoid reverse-iterating for loops.
the condition of the loop is wrong,we want loop from last index of string into first the whole range so you should use i>=0 instead of i=0
worked code :
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
string str = "PIZZA";
for(int i=str.length()-1;i>=0;i--)
cout<<str[i];
}
but you'd better know that with this code we only print string from end not Reverse IT !! to reverse we use this code :
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
string str = "PIZZA";
string rev="";
for(int i=str.length()-1;i>=0;i--)
rev+=str[i];
cout<<"Reverse = "<<rev;
}
concat items from last of the string into new one !
You would have gotten your answer without asking us if you had turned on compiler warnings:
Why should I always enable compiler warnings?
That would have given you:
<source>: In function 'int main()':
<source>:9:21: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
9 | for(int i=le-1;i=0;i--){
| ~^~
Compiler returned: 0
So the compiler is saying: "You are performing an assignment, then using the result as a boolean value/condition. Are you sure that's what you want to do?"
Now, other answers told you what to replace i=0 with (it's i>=0, or possibly i != -1). But - with the warning above, I'm pretty sure you could have reached this conclusion yourself.

Strange Number Conversion C++

So I have the following code:
#include <iostream>
#include <string>
#include <array>
using namespace std;
int main()
{
array<long, 3> test_vars = { 121, 319225, 15241383936 };
for (long test_var : test_vars) {
cout << test_var << endl;
}
}
In Visual Studio I get this output:
121
319225
-1938485248
The same code executed on the website cpp.sh gave the following output:
121
319225
15241383936
I expect the output to be like the one from cpp.sh. I don't understand the output from Visual Studio. It's probably something simple; but I'd appreciate it nonetheless if someone could tell me what's wrong. It's has become a real source of annoyance to me.
The MSVC uses a 4Byte long. The C++ standard only guarantees long to be at least as large as int. Therefore the max number representable by a signed long is 2.147.483.647. What you input is too large to hold by the long and you will have to use a larger datatype with at least 64bit.
The other compiler used a 64bit wide long which is the reason why it worked there.
You could use int64_t which is defined in cstdint header. Which would guarantee the 64bit size of the signed int.
Your program would read:
#include <cstdint>
#include <iostream>
#include <array>
using namespace std;
int main()
{
array<int64_t, 3> test_vars = { 121, 319225, 15241383936 };
for (int64_t test_var : test_vars) {
cout << test_var << endl;
}
}

A strange error when trying to scanf into a global int

Here is the code
#include "stdafx.h"
#include <string>
#include <clocale>
#include <stdio.h>
#include <cstdlib>
using namespace std;
int souls;
void userInput(char situation[20]) {
if (situation == "souls") {
scanf("%i", souls);
printf("%i", souls);
}
}
void main() {
setlocale(LC_CTYPE, "rus");
userInput("souls");
system("pause");
}
It brakes after I input something in my scanf() (trying to change a global int) via the console (int number for example) and drops me into an "unhandled exception"
Why is it so? I am using MS Visual Studio 2005.
In your code
scanf("%i", souls);
should be
scanf("%i", &souls);
^
scanf() needs a pointer to type as the argument to store the scanned value corresponding to the supplied format specifier.
That said, if (situation=="souls") is wrong, too. You cannot compare the contents of strings using the == operator. You need to use strcmp() for that.
Your code has several issues:
You cannot compare C strings this way: if (situation == "souls"): you are comparing the addresses of the char arrays, not their contents. You need to use strcmp (and include <cstring>) for this:
if (!strcmp(situation, "souls"))
The signature void userInput(char situation[20]) is confusing: the size 20 information is ignored and your are actually passing the address of a shorter string literal, this signature would be more appropriate:
void userInput(const char *situation)
You need to pass the address of the output variable to scanf and check the return value: scanf("%i", souls); invokes undefined behavior, it should be changed to:
if (scanf("%i", &souls) == 1) {
/* souls was assigned a value */
} else {
/* scanf failed to parse an integer */
}
The signature for main should not be void main(), it should be either:
int main()
or
int main(int argc, char *argv[])

Change all the int type to another type using #define for large program

I want to change all my int type in my program to support arbitrary position integer. I chose to use GMP.
I am thinking about is it possible to do a #define to replace all int to mpz_class.
I start by a small program
#include <iostream>
#define int long long int
using namespace std;
int main(){
// .... code
}
The compiler is already complaining about main have to return an int type.
Is it possible to add exception to #define? or this is a really bad idea to do so?
Redefining a keyword is prohibited iff you include any standard headers. Here, you included <iostream> so your program is ill-formed.
Otherwise, knock yourself out! Wait, no, don't, because this would still be really silly.
Instead, refactor your code to use some new type called, say, my_integer (but with a much better name):
typedef int my_integer;
Then, when you want to change from int to mpz_class, you just change the definition of my_integer:
typedef mpz_class my_integer;
use main without int like this:
#include <iostream>
#define int long long int
using namespace std;
main(){
// .... code
}
The simple answer: although technically possible you are not allowed to #define any of the reserved identifiers.