c++ compiling errors void function [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 8 years ago.
Improve this question
When i try to compile these errors shows up
line 5: [Error] expected initializer before 'void'
line 27: [Error] expected unqualified-id before '{' token
help me i need to send it to my doctor by 3 hours
#include<iostream>
using namespace std;
void ConvertToCelsuis (int temp)
void ConvertToFahernheit (int temp)
int main()
{
char ch;
int temp;
cout<<"enter a temprature followed by a charcter"<<endl;
cin>>temp>>ch>>endl;
if (ch=='f'|ch=='F')
ConvertToFahernheit (temp);
else if (ch=='c'|ch=='C')
ConvertToCelsuis (temp);
Return 0;
}
void ConvertToFahernheit (int temp)
{
float F;
}
{
F=(temp*1.8)+32;
cout<<F;
cout<<endl;
}
void ConvertToCelsuis (int temp)
{
float C;
{
C=((temp-32)*0.5);
cout<<C;
cout<<endl;
}
}

on these lines:
void ConvertToCelsuis (int temp)
void ConvertToFahernheit (int temp)
you are missing semicolons. After you fix this you will have to delve into other compilation problems, for eg:
cin>>temp>>ch>>endl;
That line is not going to work.

You need a semicolon at the end of your declaration.
void ConvertToCelsuis (int temp);
void ConvertToFahernheit (int temp);

Related

I get a warning when I try to run the recursion program with return keyword [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 9 months ago.
The community reviewed whether to reopen this question 9 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I was expecting 1 2 3 as output, but when I try to run this code:
#include <iostream>
using namespace std;
int fun(int x){
if (x>0){
return fun(x-1);
cout<<x<<endl;
}
}
int main()
{
int x=3;
fun(x);
return 0;
}
I get this warning:
warning: control reaches end of non-void function
Why doesn't it return the value and call fun(x-1)?
But the below code works perfectly. I get 3 2 1 as output.
#include <iostream>
using namespace std;
int fun(int x){
if (x>0){
cout<<x<<endl;
return fun(x-1);
}
}
int main()
{
int x=3;
fun(x);
return 0;
}
Once a function has return'ed, it can't execute any more code:
if (x>0){
return fun(x-1);
cout<<x<<endl; // <-- NEVER EXECUTED
}
The warning is because your function has a non-void return type, but is not return'ing any value when x is <= 0, thus causing undefined behavior.
Try this instead:
#include <iostream>
using namespace std;
int fun(int x){
if (x>0){
int ret = fun(x-1);
cout << x << endl;
return ret;
}
return 0;
}
int main()
{
fun(3);
return 0;
}
Online Demo

C++: invalid use of 'void' [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 6 years ago.
Improve this question
This code should print i = 35 as result but somehow it doesn't even compile. Why ?
#include <iostream>
using namespace std;
void increment(int &p){
p = p +10;
}
int main()
{
int i = 10;
increment(i) += 15;
cout<<"i = " <<i<<endl;
return 0;
}
No it shouldn't! increment has void as return type, that means that an expression call to this function has no value. If you want that call to be able to be used on the left part of an assignment, it must return a left-value.
Basically, when you write a=b a denotes a container but b a value.
You can try:
int &increment(int &p){
p = p +10;
return p; // return the reference passed as argument...
}
int main()
{
int i = 10;
increment(i) += 15;
cout<<"i = " <<i<<endl;
return 0;
}

Postfix notation stack C++ [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am new to C++ and I want to use a stack to evaluate an expression given as an input (2+3*5+4 for example), containing only numbers, + and *. I wrote this code but it gives me Segmentation fault: 11. Could you please help me solve this or give me a hint about what could be wrong? Thank you! (I noticed there were similar questions on this site, but I couldn't use them to solve my problem)
#include <iostream>
#include <stack>
using namespace std;
bool highPrecedence(char a, char b){
if((a=='+')&&(b=='*'))
return true;
return false;
}
int main()
{
char c = 'a';
double x;
stack<char> stack;
double v[10];
int i=0;
double res;
while(true)
{
c = cin.get();
if(c=='\n'){
while(stack.size()!=0){
if (stack.top()=='*'){
double res = v[i]*v[i-1];
i--;
v[i]=res;
stack.pop();
}
if (stack.top()=='+'){
res = v[i]+v[i-1];
i--;
v[i]=res;
stack.pop();
}
}
break;
}
if ( '0'<=c && c<='9' )
{
cin.putback(c);
cin>>x;
cout<<"Operand "<<x<<endl;
i=i+1;
v[i]=x;
}
else
{
if(c!=' ') cout<< "Operator " <<c<<endl;
if (stack.size()==0)
stack.push(c);
else{
while((!highPrecedence(stack.top(),c)) && (stack.size()!=0)){
if (stack.top()=='*'){
double res = v[i]*v[i-1];
i--;
v[i]=res;
stack.pop();
}
if (stack.top()=='+'){
res = v[i]+v[i-1];
i--;
v[i]=res;
stack.pop();
}
}
stack.push(c);
}
}
}
cout<<v[0]<<endl;
}
Using stack.top() is illegal if the stack is empty.
Change while((!highPrecedence(stack.top(),c)) && (stack.size()!=0)){
to while((!stack.empty()) && (!highPrecedence(stack.top(),c))){
The initiali value of i is not good and you are printing uninitialized variable, which has indeterminate value.
Change int i=0; to int i=-1;

Beginning of an nQueens game in c++. Where is the error located in my code? [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 8 years ago.
Improve this question
I keep getting errors when trying to compile the following code. The error is
expected ',' or ';' before '{' token.
It says there's an error on the parentheses after the bool check_row(x)
If I comment it out the same happens for bool check_col(x).
I kept looking back at my books if I didn't define my functions properly but they seem correct, logically.
This is the beginning of an nQueens game on a 4x4 board.
The Queen is represented by the number 1.
The two boolean functions are to check if the row and columns are free.
startGame() assigns 0 to all boxes, and showBoard() shows results of the board.
#include <cstdlib>
#include <iostream>
using namespace std;
int x=0, y=0;
int square[4][4];
void startGame()
{
for(x=0;x<4;x++)
{
for(y=0;y<4;y++)
{
square[x][y]=0;
}
}
}
void showBoard()
{
for(int x=0;x<4;x++)
{
if(x!=0)
{
cout<<endl;
}
for(int y=0;y<4;y++)
{
cout<<square[x][y];
}
}
cout<<endl;
}
bool check_row(x)
{
for(y=0;y<4;y++)
{
if(square[x][y]==1)
{
return false;
}
else if(square[x][y]==0)
{
if(y==3)
{
return true;
}
continue;
}
}
}
bool check_col(y)
{
for(int x=0;x<4;x++)
{
if(square[x][y]==1)
{
return false;
}
else if(square[x][y]==0)
{
if(x==3)
{
return true;
}
continue;
}
}
}
int main(){
startGame();
showBoard();
return 0;
}
bool check_col(y) isn't a valid prototype. You need to provide a type for y - for example bool check_col(int y). The same applies to bool check_row(x).
you have to specify the datatype which you are passing as a parameter in your function..considering x and y are of int type and are local, your function prototype should be
bool check_row(int x)
bool check_col(int y)
If x and y are global then there is no need to pass them..simply
bool check_row()
bool check_col()
will work as the visibility of global variables will be throughout the program, unless shadowed

Error: expect a ';' before curly bracket [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 8 years ago.
Improve this question
Hey guys my brain has officially gone mush with all these projects since i can seem to figure out this relatively simple bug.
So the issue that i am having is that it is asking me to add a semi-colon before the curli bracket, here is the piece of code where it is happening.
bool IsAlive(int row, int col){
return true;
}
It is asking me a semi-colon right after 'col)'
Here is the full code
#include <iostream>
#include <windows.h>
#define NumGen 1
#define NumRows 13
#define NumCol 13
using namespace std;
void main()
{
const int NumModRows=NumRows+2;
const int NumModCol=NumCol+2;
int grid[NumGen][NumModRows][NumModCol]={0};
grid[NumGen][0][0]=5;
//cout<<"Hey this is number "<<grid[NumGen][0][0]<<endl;
//int upLeft=-1, upMid=-1, upRight=-1, left=-1, right=-1, botLeft=-1, botMid=-1, botRight=-1;
//cout<<"All: "<<upLeft<<", "<<upMid<<", "<<upRight<<", "<<left<<", "<<right<<", "<<botLeft<<", "<<botMid<<", "<<botRight<<endl<<endl;
//get input from user
int rowInput;
int colInput;
int numInputs;
cout<<"how many inputs will be inserted in the grid?(in row then col format)"<<endl;
//get user inputs where the gen 0 cells are alive
for(int i=0; i<numInputs;i++)
{
cout<<"The row of input"<<endl;
cin>>rowInput;
cout<<"The col of input"<<endl;
cin>>colInput;
grid[NumGen][rowInput][colInput]=1;
}
//bool isAlive(int, int);
bool IsAlive(int row, int col){
return true;
}
//cin.get();
Sleep(10000);
}
You can't define another function isAlive inside main.
You may try to move the definition of the isAlive function outside main, then call the function inside main whenever necessary.
Is alive is declared inside the main function
void main()
{
//bool isAlive(int, int);
bool IsAlive(int row, int col){
return true;
}
}
Move it outside
//bool isAlive(int, int);
bool IsAlive(int row, int col){
return true;
}
int main()
{
}