Cannot create 2 functions outside main [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
I've started C++ not too long ago, and I have problems understanding why I can'T seem to be able to create two functions outside my main. When I only have 1, all is good, the second I add the second one, which is the Far one, tell me to put a ; after my cel function declaration...
// Lab03.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
int main()
{
double celcius(int);
double far(int);
std::cout<<"DEGREE DE FAR A CEL\n";
for (int i=32; i<213; i++)
{
std::cout.precision(3);
std::cout<<i<<"F = " <<celcius(i)<<"C ";
if ((i+1)%4==0)
{
std::cout<<"\n";
}
std::cout<<"\n\n\nDEGREE DE CEL A FAR\n";
for (int i=0; i<101; i++)
{
std::cout.precision(3);
std::cout<<i<<"C = " <<far(i)<<"C ";
if ((i+1)%4==0)
{
std::cout<<"\n";
}
}
_gettch();
return 0;
}
double celcius(int n)
{
double endcel;
endcel= (n-32.0)*(5.0/9.0);
return endcel;
}
double far(int o)
{
double endfar=(o*(9/5))+32;
return endfar;
}

It looks like you're missing an end } to close your main function just prior to the celcius function.
Proper code indentation will help you find problems like this in the future.
// Lab03.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
int main() {
double celcius(int);
double far(int);
std::cout<<"DEGREE DE FAR A CEL\n";
for (int i=32; i<213; i++) {
std::cout.precision(3);
std::cout<<i<<"F = " <<celcius(i)<<"C ";
if ((i+1)%4==0) {
std::cout<<"\n";
}
std::cout<<"\n\n\nDEGREE DE CEL A FAR\n";
for (int i=0; i<101; i++) {
std::cout.precision(3);
std::cout<<i<<"C = " <<far(i)<<"C ";
if ((i+1)%4==0) {
std::cout<<"\n";
}
}
_gettch();
return 0;
}
}
double celcius(int n) {
double endcel;
endcel= (n-32.0)*(5.0/9.0);
return endcel;
}
double far(int o) {
double endfar=(o*(9/5))+32;
return endfar;
}

Related

No Matching Function To Call Error, and i don't know why [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 3 months ago.
Improve this question
I'm having an error that the function that I'm calling doesn't exist and I don't know why. It seems to have something to do with pointers but we haven't learned pointers. I call the function and wrote it and declared it (I'm mostly just typing so I can post this at this point)
Here is my code
/*
* Program to validate a color and show its index
*
* Name: Rebecca Sakson
* Date: November 13, 2022
*/
#include <iostream>
using namespace std;
const int NUM_COLORS = 5;
int findColorIndex (string findMe, string list[], int index[]);
int main()
{
string colors[NUM_COLORS] = { "red", "green", "blue", "yellow", "purple"};
int index[NUM_COLORS] = {0,1,2,3,4};
string findMe;
int colorIndex;
//int idkWhy = NUM_COLORS;
cout << "Color?" << endl;
cin >> findMe;
colorIndex = findColorIndex(findMe, colors, NUM_COLORS);
if (colorIndex <= 0)
{
cout << "Color is valid, found at " << colorIndex << endl;
}
else
{
cout << findMe << " is not valid";
}
return 0;
}
int findColorIndex (string findColor, string list[], int index[])
{
bool found = false;
int functionIndex = 0;
while ((!found) && (functionIndex < *index))
{
if (list[functionIndex] == findColor)
{
found = true;
}
else
{
functionIndex++;
}
}
if (!found)
{
functionIndex = -1;
}
return functionIndex;
}
I think you meant to type this:
findColorIndex(findMe, colors, index);
Instead of passing NUM_COLORS, which is an int, not an int array.

What is wrong with my implementation of quicksort? [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
The program below is for sorting a list using quicksort C++.The code typed below has compiled successfully in code::blocks and in http://cpp.sh/, but unfortunately it hangs after entering in the elements,any help will be appreciated..
void quicksort(vector<int> v,int left_index,int right_index)
{
if(left_index>=right_index)
return;
int pivot=(right_index+left_index)/2;
int left=left_index;
int right=right_index;
while(left<=right)
{
while(v[left]<v[pivot])
left++;
while(v[right]>v[pivot])
right--;
if(left<=right)
{
swap(v,left,right);
left++;right--;
}
}
quicksort(v,left_index,right);
quicksort(v,left,right_index);
}
Passing by reference is must as others have pointed out.
Keep pivot constant during a partition. pivot = v[pivot] ensures that.
outer loop bounds changed to left<=right from left<right.
The running code.
#include <iostream>
#include<vector>
using namespace std;
void print(const vector<int> &v)
{
cout<<"The sorted list is:"<<endl;
for(int i=0;i<(int)v.size();i++)
cout<<v[i]<<' ';
cout<<endl;
}
void swap(vector<int> &v,int left,int right)
{
int temp=v[left];
v[left]=v[right];
v[right]=temp;
}
void quicksort(vector<int> &v,int left_index,int right_index)
{
if(left_index>=right_index)
return;
int pivot=(right_index+left_index)/2;
pivot = v[pivot];
int left=left_index;
int right=right_index;
while(left<right)
{
while(v[left]<=pivot)
left++;
while(v[right]>pivot)
right--;
if(left<right){
swap(v,left,right);
left++;
right--;
}
}
quicksort(v,left_index,right);
quicksort(v,left,right_index);
print(v);
}
int main()
{
int no;
vector<int> v;
cout << "Please enter the elements in your list" << endl;
cout << "Enter 0 to exit..."<<endl;
while(cin >> no)
{
if(no==0)
break;
v.push_back(no);
}
quicksort(v,0,v.size()-1);
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;

I don't understand why this simple program is outputting gibberish? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
#include <iostream>
#include <cmath>
using namespace std;
double T;
double V;
double WC(double T, double V);
void index(double WC(double T, double V));
int main(void)
{
cout<<"Please enter your T followed by your V"<<endl;
cin>>T>>V;
cout<<index;
}
double WC(double, double)
{
if(V>4.8)
return 13.12+0.6215*T-11.37*pow(V,0.16)+0.3965*T*pow(V,0.16);
else
return T;
}
void index(double WC(double,double))
{
if (WC(T,V)<=0&&WC(T,V)>=-25)
{
cout<<"Discomfort";
}
if (WC(T,V)<-25&&WC(T,V)>=-45)
{
cout<<"Risk of skin freezing (frostbite)";
}
if (WC(T,V)<-45&&WC(T,V)>=-60)
{
cout<<"Exposed skin may freeze within minutes";
}
if (WC(T,V)<-60)
{
cout<<"Exposed skin may freeze in under 2 minutes";
}
}
I don't understand why this is outputting random gibberish like "010F11B8", it's only supposed to print something based on input of temperature and wind velocity.
You're not calling index, you're printing its address. Method calls require parens index().
I think what you're looking for is this:
working example
to call the index function you need to do this:
#include <iostream>
#include <cmath>
using namespace std;
double T;
double V;
double WC(double T, double V);
void index(double WC(double T, double V));
int main(void)
{
V = 5.0;
T = -60.0;
// declare a function pointer which accepts two doubles and returns a double
double (*wcPtr)(double, double);
// initialise function pointer
wcPtr = &WC;
cout << "Please enter your T followed by your V" << endl;
// call function pointer
index(wcPtr);
}
double WC(double T, double V)
{
if(V>4.8)
return 13.12+0.6215*T-11.37*pow(V,0.16)+0.3965*T*pow(V,0.16);
else
return T;
}
void index(double WC(double T,double V))
{
if (WC(T,V)<=0&&WC(T,V)>=-25)
{
cout<<"Discomfort";
}
if (WC(T,V)<-25&&WC(T,V)>=-45)
{
cout<<"Risk of skin freezing (frostbite)";
}
if (WC(T,V)<-45&&WC(T,V)>=-60)
{
cout<<"Exposed skin may freeze within minutes";
}
if (WC(T,V)<-60)
{
cout<<"Exposed skin may freeze in under 2 minutes";
}
}

C++ reading user inputs comparing array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have this code where it read user code and store it on array, and later find sums of elements on each array and compare both of them, the code is as follows:
#include <iostream>
#include <vector>
#include <numeric>
typedef std::vector<int> int_vec_t;
//Call by reference to set variables in function
void readData(int_vec_t& v1, int_vec_t& v2)
{
v1 = int_vec_t{1,1,8}; //This only works for C++11
v2 = int_vec_t{2,2,2};
}
void readUserData(int_vec_t& v)
{
for(;;)
{
int val;
std::cin>>val;
if(val == 0) break;
v.push_back(val);
}
}
int main()
{
using namespace std;
int_vec_t A;
int_vec_t B;
readData(A,B);
//Or
readUserData(A);
readUserData(B);
int sumA = accumulate(A.begin(), A.end(), 0); //Then use iterators
int sumB = accumulate(B.begin(), B.end(), 0);
cout << ((sumA > sumB) ? "Array A Greater Than Array B\n" : "Array B Greater Than Array A\n");
return 0;
}
But the above code generate following errors:
test.cpp: In function ‘void readData(int_vec_t&, int_vec_t&)’:
I am using g++ test.cpp -o test to compile the code. What am I missing here?
don't you think the compilation should be something like:
$ g++ -std=c++11 test.cpp -o test ?
it keep taking inputs, how can i limit it to take only 5 elements per array
void readUserData(int_vec_t& v)
{
for(int i = 0; i < 5; i++)
{
int val;
std::cin>>val;
// if(val == 0) return;
v.push_back(val);
}
}
Great thanks, in between the loop how can out put statement so that user knows he is entering array for array 1 and after that array 2?
void readUserData(int_vec_t& v, std::string default = "")
{
for(int i = 0; i < 5; i++)
{
int val;
std::cout << "Enter for "<< default << "[" << i << "]: ";
std::cin>>val;
// if(val == 0) return;
v.push_back(val);
}
}
And from your main() you can send in a different string.
for example like this:
readUserData(A, "A");
readUserData(B, "B");
This was my code posted as a suggested solution to
c++ passing function into main error
Yes it needs a C++11 compiler as stated in the code. It was never meant as final code either.
I would recommend the OP to read a good book about C++. Let me suggest "C++ The Programming Language" http://www.amazon.com/The-Programming-Language-3rd-Edition/dp/0201889544