error: lvalue required as increment operand without an error line - c++

I've seen a similar post on StackOverflow about this error code, but theirs seemed to have an error code line, and they were also attempting something slightly different in their function.
I'm trying to count the number of a specified character in a string (inputting p and apple would return 2, for example). At some point, my method broke down. I've been scouring my code for what specific increment operator is causing this, but without an error code line I don't know which one is causing it, and I've messed around with both.
I'm not sure which of these variables isn't modifiable, but all it tells me is:
main.cpp:11:30: error: lvalue required as increment operand
If anyone could take a look at my code and tell me what's wrong, I would really appreciate it.
#include <iostream>
#include <string>
using namespace std;
int main() {
string b;
char a;
int i;
int counter(string b) {
counter = 0;
for(int i = 0; i < b.length(); i++)
if (b[a] == a), counter++);
counter = i;
return counter;
}
cin >> a;
cin >> b;
return counter(b);
}
return 0;
}
PS, I know this code has multiple other errors. It's a work in progress, but I am trying to focus on this operand issue first.
I've been trying to redefine variables for both counter and i, as I'm not sure which one is causing the issue, but nothing has fixed it. For example, within the for loop, I tried setting counter equal to some variable, but that didn't help. I think it's possible I messed up the syntax too within the for loop, and I spent some time in there, but if that's the issue I didn't fix it.

The program is invalid
In this line
int counter(string b); {
^^^
you declared a function with the name counter (see the semicolon).
After the function declaration there is followed a compound statement
int counter(string b); {
counter = 0;
for(int i = 0; i < b.length(); i++)
if (b[a] == a), `counter++`);
return counter;
}
within which the function name counter is used as a variable as for example counter++
So the compiler issues the error message
"main.cpp:11:30: error: lvalue required as increment operand".
because you are using the function name and trying to apply to it the post-increment operator ++. But the function designator in this case in implicitly converted to a pointer to the function and it is not an lavlue.
After the compound statement you are calling the function counter though it is not defined
return counter(b);
It seems you are trying to define the function counter within the function main. You may not do that.
Also the function should accept two arguments: the string and the character.
Your program can look the following way
#include <iostream>
#include <string>
size_t counter( const std::string &s, char c )
{
size_t count = 0;
for ( char ch : s )
{
if ( c == ch ) ++count;
}
return count;
}
int main()
{
std::string s;
char c;
std::cin >> s;
std::cin >> c;
std::cout << counter( s, c ) << '\n';
}

Related

Is there any backlash to do comparison operator on constexpr, const, and non-const?

Basically the title, I'm still learning C++ and want to know how this work.
example:
int i = 1;
const int ci = 2;
constexpr int cei = 1;
if (i != ci && i == cei)
cout<< i << endl;
Vs
int i = 1;
int ci = 2;
int cei = 1;
if (i != ci && i == cei)
cout<< i << endl;
Is there any difference?
Yeah! there is difference between the two section you mentioned but in your context there is no difference in comparison because we are comparing both values not the type of variables so it will satisfy the equation.
If you want to make more depth discussion on your question then you can read the rest lines :
From the first you can see that you had used const int, it will throw an error if you'll try to assign other values to it because the values mentioned with const keyword can't be manipulated.
eg:-
#include <iostream>
using namespace std;
int main()
{
const int a = 5;
a = a+5;
cout<<a;
return 0;
}
This will throw an because error firstly we are declaring const and then manipulating it.
Error will be like : assignment of read-only variable ‘a’.
Now comming to constexpr indicates that the value, or return value, is constant and hence this will works as similar to the const keyword and will throw the same error if we'll manipulate it after defining.
But int without const or constexpr will be mutable and can be changed whenever we wants to change in the program.

How to fix this error "expected primary expression before 'int'"?

I am a newbie programmer in c++ started my Computer Science degree. I got a problem with my assignment.
You need to make a function char calculate_daily_sale(int []).
You need to define and read an array of integer values of length 10 in the main() function.
Write a function charcalculate_daily_sale (int []) that accepts the array as argument from the main() function. The function will sum the values of array. If the values are greater than or equal to 15,000 the
function return ‘y’ back to the main function, otherwise ‘n’ is returned from the function.
This is the code that I managed to write:
#include <iostream>
using namespace std;
char calculate_daily_sale(int[])
{
int arr[10];
int *ptr[10];
int sum=0;
for(int j=0; j<=9; j++)
{
ptr[j]=&arr[j];
sum=sum+arr[j];
cout<<sum;
}
}
int main()
{
int n,y;
int arr[10];
int *ptr[10];
for(int i=0; i<=9; i++)
{
ptr[i]=&arr[i];
cin>>*ptr[i];
}
if(calculate_daily_sale(int[])>=15000)
{
return y;
}
else
{
return n;
}
return 0;
}
The error that I am getting is:
expected primary expression before 'int'
You need to take a step back and learn the basics of C++ programming.
Some points you should be looking at are:
char calculate_daily_sale(int[])
The function has a return type of 'char' and therefore needs a return statement.
The function parameter is not named and not used. It can be removed entirely.
if(calculate_daily_sale(int[])>=15000)
When calling a function, you need to pass a value, not a type 'int[]'
The return type of is char so it seems odd to be comparing it with 15000.
return y and return n
n and y are uninitialized.
A value returned from main is simply an error code returned to the operating system that runs the programme. It seems unlikely that you want to return these numbers, whatever they are. My reading of the spec is that you need to be returning the characters 'n' and 'y' (for 'yes' and 'no') from your calculate_daily_sale and to main, which is why the return type is char.
Error messages always mention line number. This is the way you can locate the error precisely. Assuming your error is in this line
if(calculate_daily_sale(int[])>=15000)
You probably meant to pass the array arr to calculate_daily_sale:
if(calculate_daily_sale(arr)>=15000)

Is there a way to fix this error : no match for 'operator[]' (operand types are 'empl' and 'int')

Im getting homework done but i faced this problem i tried everything changing variables name, changing function ....
I looked into this problem on google still no idea how to fix this error.
#include<stdio.h>
#include<iostream>
struct empl {
char nom;
char pre;
float salaire;
double cin;
}empl;
struct empl t[50];
struct empl E;
int taille(int n)
{
printf("saisie la taille de tableaux\n");
scanf("%d\n", &n);
return 0;
}
int remplire(int n, struct empl t, int i)
{
for (i = 1; i <= n; i++)
{
printf("t[%d].nom= ", i);
scanf("%s\n", &t[i].nom);
printf("t[%d].prenom= ", i);
scanf("%s\n", &t[i].pre);
printf("t[%d].salaire= ", i);
scanf("%f\n", &t[i].salaire);
printf("t[%d].CIN= ", i);
scanf("%lf\n", &t[i].cin);
}
}
int main()
{
int i, n;
int taille(int n),
taille(n);
int remplire(int n, struct empl t, int i);
remplire(n, t, i);
}
Although your code is written mostly in C style, you seem to be compiling it with a C++ compiler, as it accepts #include <iostream> (though you do not appear to use anything from it), and it has a notion of operator overloading. C and C++ are distinct languages, and the distinction is somewhat relevant here.
In either language, however, the code you have presented is flawed. The problem is with the several expressions in function remplire that follow this pattern: &t[i].nom. Absent any operator overloading (which is not available in C anyway), the [] operator in those expressions requires one of its operands to designate either a pointer or an array, and the other to designate an integer. Although there is a file-scope variable t that is an array, inside remplire() that is shadowed by a function parameter with the same name. Inside that function, then, t refers the the parameter, which is a struct empl, not an array or pointer (or integer).
Your compiler ought to be giving you another clue, too, where you call that function ...
remplire(n,t,i);
..., passing the global t as an argument. The compiler very much should complain about a type mismatch between the second argument (t) and the corresponding function parameter.
Perhaps what you wanted to do is simply to declare remplire() to accept a structure pointer as its second parameter:
int remplire(int n, struct empl *t, int i)
While you're at it, do remove the redundant local declaration of that function inside main(). You don't need that as long as remplire() is defined before main(), and if you want to have a separate declaration of that function then it would best be placed at file scope, and probably in a header file.
First of all iostream is C++ header coming from standard library and will not work for C program. And now issues:
int taille (int n)
{
printf("saisie la taille de tableaux\n");
scanf("%d\n",&n);
return 0;
}
This function is called with an input parameter - that means you can pass a value into a function, but not access the parameter and hope it will be used in other places. To correct this you should declare the function should look like this:
int taille (int * n)
{
printf("saisie la taille de tableaux\n");
scanf("%d\n", n);
return 0;
}
Next function - similar problem, it should look like this:
int remplire (int n , struct empl * t ,int i)
{
for (i=1;i<=n;i++)
{
printf("t[%d].nom= ",i);
scanf("%s\n",&t[i].nom);
printf("t[%d].prenom= ",i);
scanf("%s\n",&t[i].pre);
printf("t[%d].salaire= ",i);
scanf("%f\n",&t[i].salaire);
printf("t[%d].CIN= ",i);
scanf("%lf\n",&t[i].cin);
}
}
Or even like this:
int remplire (int n , int i)
as t is global variable. Also this function should return some value as it is declared to return int.
And now the main function:
int main()
{
int i,n;
int taille(int n),
taille(n);
int remplire(int n,struct empl t,int i);
remplire(n,t,i);
}
Don't redeclare functions inside another function, even if it is permissible it does not mean you should do it. Also main function should return 0 if everything works fine. To correct the function write it like this:
int main()
{
int i,n;
taille(& n);
remplire(n,& t,i);
}
Some good advice, please read some books to learn how to program in C if you want to go that way.
There are many issues in your code.
Starting reading a good C textbook is advised.
You probably want this:
#include <stdio.h>
#include <iostream>
struct empl {
char nom[30];
char pre[30];
float salaire;
double cin;
}empl;
struct empl t[50];
struct empl E;
int taille(int & n)
{
printf("saisie la taille de tableaux\n");
scanf("%d", &n);
return 0;
}
void remplire(int n, struct empl *t)
{
for (int i = 0; i < n; i++)
{
printf("t[%d].nom= ", i);
scanf("%s", &t[i].nom);
printf("t[%d].prenom= ", i);
scanf("%s", &t[i].pre);
printf("t[%d].salaire= ", i);
scanf("%f", &t[i].salaire);
printf("t[%d].CIN= ", i);
scanf("%lf", &t[i].cin);
}
}
int main()
{
int n;
taille(n);
remplire(n, t);
}
It's still poor code and it's written mostly in C style, but it compiles and works as intended.
In C++ you'd do this totally differently.
You've declared this global array variable t
struct empl t[50];
and also declared a parameter t in this function
int remplire (int n , struct empl t ,int i)
Inside the function it's going to treat any instance of t as to be the parameter as that is nearer in scope than the global variable. So when you have code like this...
scanf("%s\n",&t[i].nom);
...it's going to throw up errors because t isn't an array.
The solution is to use variable names that have meaning like "employee_array" rather than single letters.
Also that call to scanf is wrong as for strings you don't need to pass in a pointer to the variable, so it should look like
scanf("%s\n",t[i].nom);
But you'd also need to make nom be a string too - currently it's only a char.

What happens to a parameter if it is passed twice? Once by value and once by reference? Will it be modified or not?

#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("BAC.TXT");
void eval(int a, int b, int &rez)
{
rez = a + b;
}
int main()
{
int nr;
int s;
fin >> s;
while (fin >> nr)
eval(s, nr, s);
cout << s << '\n';
return 0;
}
So I have this code snippet. I am reading numbers from a file and keeping track of their sum using a given function called “eval”. I know it could be considered bad code to pass a parameter twice (in such a given instance) rather than using another variable (not sure, though, if it is bad code or not, in my case). My problem is: will it change the value of variable s? Again, I am passing it once by value and once by reference! I have written the code on my PC and it does change the value of s. Now my question would be: why? If I am asking this in a right way: what happens “in the background”?
The fact that a is a copy of s is actually a red herring. Maybe that caused your confusion, but its just a copy. Consider that you could call the function like this
auto temp = s;
eval(temp,nr,s);
to get the exact same result. Inside the function rez is a reference to s, hence modifications on it will be reflected on s in main. In other words, rez is an alias for s, while a has no relation whatsoever to s, they just happen to hold the same value.
This question is related to the behavior of functions parameters and reference/pointers.
When you pass a value to a function, it copies the value you give to a local variable inside the function.
in your case, when you pass 's', it copies it to 'a' like if you would do it in your main: int a = s (this is what is going on)
The same applies to the reference. when you pass 's' to the third parameter, it does this:
int &rez = s
When you pass it by reference, it is sort of taking the address of the variable instead of the value itself and copying this address to a local variable that is already dereferenced. (pointer stuff)
So the value is changed.
See the video related to pointers and then reference on 'the Cherno Project' youtube channel for better comprehension of the subject:
Pointers
References
Short answer: Yes, s will be changed.
Long answer: Let me rewrite your code snippet a little bit to illustrate what is happening with the function parameters during a call:
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("BAC.TXT");
void eval(int a, int b, int &rez)
{
rez = a + b;
}
int main()
{
int nr;
int s;
fin >> s;
while(fin >> nr) {
int a = s;
int b = nr;
s = a + b;
}
cout << s << '\n';
return 0;
}
So, basically, a and b are just copies of s and nr respectively. However, res is s.
IMO, this is what I would expect :
Copy s to a new var l_S.
Copy nr to a new var l_Nr.
Copy &s to new var l_Ptr.
Add l_Nr and l_S.
Save the result in *l_Ptr.

Request for member 'modu18' in f, which is of non-class type 'int'

just started off with c++, and stackoverflow for that matter.
any and help infinitely appreciated, apologies in advance if i ask something super dumb
I'm making a program to solve a problem. How many 8 digit numbers are divisible by 18 and are only comprised of the digits 1, 2 and 3. I can generate numbers, but when calling a function i made to use modulus to determine whether they're divisible by 18 or not, the function gives me the error in the title. my code is as follows:
main.cpp
#include <iostream>
#include "functions.h"
using namespace std;
int main()
{
functions f();
for(int a = 1; a<4; a++){
for(int b = 1; b<4; b++){
for(int c = 1; c<4; c++){
for(int d = 1; d<4; d++){
for(int e = 1; e<4; e++){
for(int f = 1; f<4; f++){
for(int g = 1; g<4; g++){
for(int h = 1; h<4; h++){
int number = 10000000*a + 1000000*b + 100000*c + 10000*d + 1000*e + 100*f + 10*g + h; //generates 8 digit numbers, only using the digits 1 2 and 3
cout << number << endl; //prints numbers
int y = 10; //to test this bloody function
cout << f.modu18()(y); //returns 0 or 1 from func
}}}}}}}}}
functions.h
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include <iostream>
using namespace std;
class functions
{
public:
functions();
string modu18(int x);
protected:
private:
};
#endif
functions.cpp
#include "functions.h"
#include <iostream>
using namespace std;
functions::functions(){
cout << "Functions initialised!" << endl; //to see if this thing is actually loading
}
string functions::modu18(int x){ //this is checking whether the number is divisible by 18
if(x % 18 == 0){
return 0;
}else{
return 1;
};
} //btw, using multiple files becuase i want to learn how to make this work for the future
The exact error returned when compiling is
request for member 'modu18' in 'f', which is of non-class type 'int'
I have no clue why this is saying this, all my data types are correct, for the data and the function types.
send help pls
Many thanks.
In your function main there are two different f:
First one here
functions f();
is a variable of type functions or at least that is what you expect (see comments, the correct definition would be functions f;. As it is now, this would be the declaration of a function taking no arguments and returning an object of type functions).
The second one here:
for(int f = 1; f<4; f++){
is of type int.
The for loop is in a different scope than the where the first declaration is (each {} introduces a new scope) and therefore it is allowed to reuse identifiers for different objects.
If you refer to the doubly-used name, it will be looked up by certain rules and in the most common case the one in the most-inner scope will be used. Therefore here
cout << f.modu18()(y); //returns 0 or 1 from func
f is the loop counter of type int, not the f declare in the outer scope.
Use different variable names to solve this issue.
Additional stuff unrelated to error message:
Your function modu18 is supposed to return a string, but all your return statements return integer literals which are of integer type and can not be automatically cast to std::string, or rather the implicit conversion will not do what you expect it to.
You do not need classes to use multiple files. In a header file you can declare a free function with string modu18(int); and then define it in the implementation file with string modu18(int a) { ... }.
Using using namespace std; is not good practice, as it will lead to very hard to understand errors down the line. Especially in header files it should never be used. Instead specify all names from the standard library fully with std::.