I'm new on programming and I need to learn it for Arduido purposes. I used this code to test some things but I keep getting the "expected primary function before "int"" error, and it also says that the position functions isn't declared.
Am I declaring it wrong? I've tried many different things and kept getting the same message. My objective is to keep typing '1' and get 3, 6, 9, etc on the screen from calling the position function at cout.
#include <iostream>
using namespace std;
int main()
{
int degree=0;
int r=1;
while (r != '0')
{
cin >> r;
// this is where I get the error //
int position()
{
if ( r == 1 )
{
degree=degree+3;
}
return degree;
}
cout << position();
}
return 0;
}
Nesting functions is not allowed in C++. Change your code to be:
#include <iostream>
using namespace std;
int main()
{
// code
}
int position()
{
}
The problem is that your function position is in your function main, which is not possible in c++. Move the position out of the main function.
int position(int r, int degree)
{
if ( r == 1 )
{
degree=degree+3;
}
return degree;
}
int main()
{
int degree=0;
int r=1;
while (r != '0')
{
cin >> r;
cout << position(r, degree);
}
return 0;
}
Related
#include <bits/stdc++.h>
using namespace std;
struct stu {
int n;
stu(int _n = 0):n(_n) { }
int add(int a, int b = n-1) {
return a + b;
}
};
int main() {
stu obj = stu(5);
cout << obj.add(10) << endl;
}
The compiler shows the message " invalid use of
non-static data member 'stu::n' ".
What is wrong with this code. Any help would be great.
Thanks.
You can't use default arguments this way. Consider writing two separate functions:
struct stu {
int n;
int add(int a, int b) { return a + b; }
int add(int a) { return a + n - 1; }
}
yes i know other ways to count digits and returning to main function from the recursion function, but i'd like to print it in the void function. im having difficulty with it. could somebody help?
#include <iostream>
using namespace std;
void recursive_function(int num)
{
int sum=0;
while(num!=0){
recursive_function(num/10);
sum++;
}
cout<<sum<<endl;
}
int main()
{
recursive_function(345289467);
return 0;
}
If you do not want to use the return-stack to count your digits, you will need to pass the current count throughout the call stack as function parameter:
void recursive_function(int num, int count=1)
{
if (num>=10) {
recursive_function(num/10, count+1);
} else {
cout<<count<<endl;
}
}
your recursive function should return integer.
#include <iostream>
using namespace std;
int recursive_function(int num)
{
if(num>9){
return 1+recursive_function(num/10);
}else
return 1;
}
int main()
{
cout << recursive_function(123456789);
return 0;
}
#include<iostream>
using namespace std;
int c;
int fun_div();
int fun_div(int a,int b){
if(a%b==0){
c=1;
cout<<"Solution Available :\t"<<c;
} else
{
c=0;
}
return c;
}
int main(){
int c;
int e,d;
cout<<"enter two values : \n";
cin>>e>>d;
cout<<endl;
}
error in finding the mod of two numbers and not compiling the program :
error in finding the mod of two numbers and not compiling the program
It compiles for me
#include<iostream>
using namespace std;
int c;
int fun_div();
int fun_div(int a,int b)
{
if(a%b==0){
c=1;
cout<<"Solution Available :\t"<<c;
} else {
c=0;
}
return c;
}
int main(){
int c;
int e,d;
cout<<"enter two values : \n";
cin>>e>>d;
fun_div(e,d);
cout<<endl;
}
You should put the error message when asking about compilation errors. However I copied your code exactly and it compiles.
The other thing is that you don't call your function so I added that as well.
And as a side note, you could just do
int fun_div(int a, int b)
{
return (a%b == 0);
}
because (a%b == 0) will evaluate to 1 if a is a multiple of b and 0 otherwise.
#include<iostream>
using namespace std;
int c;
int fun_div();
int fun_div(int a,int b)
{
if(a%b==0){
c=1;
cout<<"Solution Available :\t"<<c;
}
else
{ c=0; }
return c;
}
int main()
{
int e,d;
cout<<"enter two values : \n";
cin>>e>>d;
c=fun_div(e,d);
cout<<endl;
}
Try this. I think this is what you expected.
Please explain your question to get a more specific answer.
I have added a function call to the function fun_div.
you should probably add one more check for which is greater.. the greater check will ensure that there is a proper remainder available
Well the main problem in your code is that you are not calling the function which you defined, that is why you are not getting the desired result, And there are some better practices for writing code which you should follow to avoid errors in future.
Don't use global variable and if you are returning the result from function then show on screen from main function.
Recommended Code is given below, i have changed the function so it will only check that 'a' is divisible by 'b' or not and return the value to main so it will show the result on screen.
#include<iostream>
using namespace std;
int fun_div(int a, int b)
{
return (a%b == 0);
}
int main() {
int e, d;
cout << "enter two values : ";
cin >> e >> d;
if (fun_div(e, d))
{
cout << "Solution Exists.";
}
else
{
cout << "No Solution Exists.";
}
cout << endl;
return 0;
}
#include<iostream>
using namespace std;
class ulam
{
int num;
double prod;
int cot;
public:
ulam(){cot=0;}
ulam(int x)
{
num=x;
}
void process()
{
for(int i=0;num==1;i++)
{
cout<<num<<endl;
if((num%2) == 0)
{
prod=num/2;
}
else
{
prod=(3*num)+1;
}
num=prod;
cot++;
}
}
void display()
{
cout<<"the number of steps required is: "<<cot;
}
};
int main()
{
int n;
cout<<"enter the number"<<endl;
cin>>n;
ulam obj(n);
obj.process();
obj.display();
}
when i write this code the cot value comes as a garbage value i think. i cant figure out where i went wrong. i used class because i feel it is more discriptive . but the main aim behind this program is to find the number of steps it is required for a number to reach one and to print the whole sequence of numbers. for thos who dont know about the collatz conjecture https://en.wikipedia.org/wiki/Collatz_conjecture
Your condition of the for loop inside process function is wrong. it should be num!=1. You need to initialize cot too. You don't need prod actually.
#include<iostream>
using namespace std;
class ulam
{
int num;
int cot;
public:
ulam()
{
cot=0;
}
ulam(int x)
{
cot=0;
num=x;
}
void process()
{
for(int i=0;num!=1;i++)
{
cout<<num<<endl;
if((num%2) == 0)
{
num=num/2;
}
else
{
num=(3*num)+1;
}
cot++;
}
}
void display()
{
cout<<"the number of steps required is: "<<cot;
}
};
int main()
{
int n;
cout<<"enter the number"<<endl;
cin>>n;
ulam obj(n);
obj.process();
obj.display();
return 0;
}
First Problem
In the constructor where you initialize when an integer is passed, you ALSO have to initialize cot like this:
ulam(int x)
{
cot = 0;
num = x;
}
Better yet, since cot is always going to be 0 at construction, just set cot to 0 as a private variable like this:
class ulam
{
int num;
double prod;
int cot = 0;
public:
//....
};
This means that you could still have a default constructor that will do nothing, and the one that takes an integer won't require cot to be set to 0.
Second Problem
Your second problem is that the loop condition is wrong. It should be num != 1, not num == 1. num == 1 would be the loop would never run unless 1 was passed in cin.
I know this error is because i have declared stu inside the for loop scope but its the necessity of the program.I want to declare an array for each test case (test case should all be input at once).Suggest me a way to achieve this.Is dynamic memory an alternative.
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
int t;
cin>>t;
int n[t],g[t];
int m =0;
for(int w=0;w<t;t++)
{
cin>>n[w]>>g[w];
int stu[n[w]];
for(int i=0;i<n[w];i++)
{
cin>>stu[i];
}
}
while(m<t)
{
int a,b;
int e;
e = (n[m]*(n[m]-1))/2;
int diff[e];
if (g[m]=1)
{
cout<<0<<endl;
return 0;
}
b=*(min_element(stu,stu+n[m]-1));
a=*(max_element(stu,stu+n[m]-1));
if (g[m]=n[m])
{
cout<<a-b<<endl;
return 0;
}
int z = 0;
for(int j=0;j<(n[m]-1);j++)
{
for(int k=(j+1);k<n[m];k++)
{
diff[z]=abs(stu[j]-stu[k]);
++z;
}
}
cout<<*(min_element(diff,diff+e-1))<<endl;
++m;
}
cin.ignore();
cin.get();
return 0;
}
You are declaring stu inside of a for loop, so it is limited to the scope of the loop. You then try to use it outside of the loop, where it is undeclared.
for(int w=0;w<t;t++)
{
...
int stu[n[w]]; // Beware: stu is a VLA. Non-standard C++.
// OK to use stu here
...
}
// stu doesn't exist here
Also note that standard C++ does not support variable length arrays (VLAs), which is what you are attempting to use in the declaration of stu, as well as here:
int t;
cin>>t;
int n[t],g[t];
You can replace these arrays by std::vector<int>:
#include <iostream>
#include <vector>
int main()
{
int t=0;
cin>>t;
std::vector<int> n(t);
std::vector<int> g(t);
std::vector<int> stu ...;
}
The line
int stu[n[w]];
is inside a block and outside that block it won't be seen. You should move it out of the block, but doing so of course you can't use n[w], being w the looping var. You coudl put a limit to the max value n[w] can have, e.g.
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAXV = 100;
int main()
{
int t;
cin>>t;
int n[t],g[t]; // <- supported by some compiler, but not std
int m =0;
int stu[MAXV];
for(int w=0;w<t;t++) {
cin>>n[w]>>g[w];
for(int i=0;i<n[w] && i < MAXV;i++) {
cin>>stu[i];
}
}
while(m<t) {
int a,b;
int e;
e = (n[m]*(n[m]-1))/2;
int diff[e];
if (g[m]==1) {
cout<<0<<endl;
return 0;
}
b=*(min_element(stu,stu+n[m]-1));
a=*(max_element(stu,stu+n[m]-1));
if (g[m]==n[m]) {
cout<<a-b<<endl;
return 0;
}
int z = 0;
for(int j=0;j<(n[m]-1);j++) {
for(int k=(j+1);k<n[m];k++) {
diff[z]=abs(stu[j]-stu[k]);
++z;
}
}
cout<<*(min_element(diff,diff+e-1))<<endl;
++m;
}
cin.ignore();
cin.get();
return 0;
}
(I've fixed a couple of assignment in conditional when I suppose you meant == and not =, but I've not tested if the code does what you expect: it just compile, with g++ but not with other compiler likely, see comment in code)