Assign a function to a new variable [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Why can't I assign my new_age(age) to another variable (in this case it will be new)?
I get the following errors:
main.cpp:21:9: error: expected type-specifier before ‘=’ token
21 | new = new_age(age)
| ^
This is my code:
#include <iostream>
using namespace std;
int new_age (int & age)
{
return (age + 100);
}
int main()
{
int age {};
new = new_age(age)
cout << "How old are you "
cin >> age
cout << "In 100 years you will be " << new:
return 0;
}

The word new is a keyword in C++. It has a special meaning and cannot be used as name of a variable.
For a full list of all keywords see here.
As an aside, even if new was allowed as an identifier, you have not declared it. To declare and initialize it directly with the value you need to add the variable's type in front of the identifier:
int not_new = new_age(age);
(Also note the semicolon at the end of the declaration which you forgot or mistyped in a lot of statements as well.)
It also seems that you'd want to call new_age(age) after taking input from the user.

Related

cout macro value doesn't work as expected [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 months ago.
Improve this question
#include<iostream>
using namespace std;
#define C 1<<(8*1)
int main(){
if(C==256){
int a=C;
cout<<a;
}
}
My expectation is 256 but it print 18. What's wrong with it? Thanks!
I assume your question is about std::cout << C;, not about std::cout << a;.
Macros are simple copy-and-paste text replacement. When preprocessor encounters macro name, it replaces it with the definition as text without any analysis. So, what happens is that
std::cout << C;
is replaced with
std::cout << 1<<(8*1);
which should indeed print 18.
That's one very good reason to not use macros. Instead, use a constant (or constexpr) variable:
constexpr int C = 1 << (8 * 1);
This is type safe and will never surprise you when used in any context.
If you really have to use macro for some reason, make sure to wrap it in extra parantheses:
#define C (1 << (8 * 1)) // but seriously, don't use macros

Finding the size of datatypes in C ++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I tried to write a program like this
#include<iostream>
using namespace std;
int main(){
int a ;//declaration
a = 12 ;//initialization
cout<<"size of int"<<size of (a)<<endl;
return 0;
}
and the output came like this
datatypes.cpp: In function 'int main()':
datatypes.cpp:8:26: error: 'size' was not declared in this scope; did you mean 'size_t'?
8 | cout<<"size of int"<<size of (a)<<endl;
| ^~~~
| size_t
[Done] exited with code=1 in 17.278 seconds
How to solve it ?
There is no size of () function/operator in C++, there is only sizeof. C++ doesn't allow names to have spaces.
Note: sizeof is an operator, not a function.
You should change this to:
#include<iostream>
// using namespace std; is bad
int main(){
int a;
a = 12;
// sizeof is an operator, so you can do sizeof a.
// the parentheses aren't actually needed
// use newline character instead of std::endl
std::cout <<"size of int" << sizeof (a) << '\n';
return 0;
}

What does error "redefinition of formal parameter" mean and how can I fix it? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
C++ beginner here with a functions error, I get the error "redefinition of formal parameter". What does this mean? and how can I fix it.
int getGuessFromUser(int guess)
{
std::cout << "Guess my lucky number between 0 and 10: ";
int guess;
std::cin >> guess;
return guess;
}
You have a parameter int guess and a variable int guess
You don't seem to be using the parameter, so perhaps remove it?
int getGuessFromUser()
{
std::cout << "Guess my lucky number between 0 and 10: ";
int guess;
std::cin >> guess;
return guess;
}
You are getting this error because you have already declared the parameter guess in you getGuessFromUser function. You do not need to declare it again, so you can remove the line int guess
You have a function parameter named guess and also a local variable with the exact same name.
This would be just like declaring two variables with the same name in the same scope (like a function), which is not allowed.
Change one of the names to remove the error.

Why is this string not declared in the scope? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm experimenting with classes and I'm wondering why I'm getting an error saying "calvin" is not defined in the scope. Thanks.
#include <iostream>
#include <string>
using namespace std;
class people
{
public:
string name;
int age;
};
int main()
{
people peeps[10];
peeps[1].name = calvin;
peeps[1].age = 21;
cout << peeps[1].name << peeps[1].age;
}
Without quotes, calvin is a variable (which is undefined). You should make it a literal (i.e. "calvin").
firstly i would suggest if you had made and age private class members and also create get name/age and set name/age member functions to protect your data and finally
peeps[1].name = calvin;
//calvin is an undefined variable
you should have used string literals for example
peeps[1].name = "calvin";

printf working fine when kept outside of for loop but causes some error while running when kept in the for loop [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int b;
printf("hello");
for(b=1;b<=100;++b)
{
if(b%10==1){
cout << "\n";
for(int l=0;l<=100;++l)
cout << "-" ;
cout << endl;
}
printf("|%s|",b);
}
return 0;
}
enter image description here
printf which is placed outside of loop body works fine but the one placed in the loop body of for causes some kind of error while running!! take a look at the picture !
Your b is an int.
You give b where printf() expects a pointer to char and will attempt to dereference the value you give as such.
Since the value you give via b is not a valid pointer to anything, your program has some access problem.