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

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";
}
}

Related

wrong output on printing subsets of array [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 1 year ago.
Improve this question
I am trying to print all the subsets of an array. But not getting the correct output.
#include<iostream>
using namespace std;
int arr[]={1,2,3,4,5};
int n=5;
void print(int a[],int cnt, int idx)
{
if(idx==n)
{
for(int i=0;i<cnt;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
return;
}
a[cnt]=arr[idx];
print(a,cnt,idx+1);
print(a,cnt+1,idx+1);
}
int main()
{
int a[5]={0};
print(a,0,0);
}
the above code only prints "5"
Please help me to rectify the same.
Replace the array with a vector. As the array in C++ is passed by reference, the print function is accessing the same array, which causes the problem.
#include <iostream>
#include<vector>
using namespace std;
int arr[]={1,2,3,4,5};
int n=5;
void print(vector<int>a,int cnt, int idx)
{
if(idx==n)
{
for(int i=0;i<cnt;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
return;
}
a[cnt]=arr[idx];
print(a,cnt,idx+1);
print(a,cnt+1,idx+1);
}
int main()
{
vector<int >a(5,0);
print(a,0,0);
}

How should i solve this task? [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 1 year ago.
Improve this question
I got the homework,i try to solve it,but i dont know why its bad.
One of part of this code was a default:
#include <iostream>
using namespace std;
/*##SOLUTION##*/
/*##SOLUTION##*/
int main()
{
Valami var;
int i=var.ujszam();
cout<<i<<endl;
cin>>i;
var.tarol(i);
var.print();
return 0;
}
I have to to supplement this code with some criterion.
I have to create Valami class after that I have to create ujszam method which is get the integer number and give it back.
I have to create tarol method which is save the Integer number.
and finally i have to create print method which is display the saved number.
The input -2 and 72,the result gonna be -2 and "A szam : 72"
Here is my attempt:
class Valami
{
public:int tarol;
public:int ujszam()
{
int i;
cin>>i;
return i;
}
public:void tarol(int ertek)
{
tarol=ertek;
}
public:void print()
{
cout << "A szam : " <<tarol<< endl;
}
};
I try to fix it some many ways,but idk what is the problem.
tarol is used as name of both a variable and a function. You have to give another name to one of them.
example:
class Valami
{
public:int tarol_var; // rename the variable
public:int ujszam()
{
int i;
cin>>i;
return i;
}
public:void tarol(int ertek)
{
tarol_var=ertek; // rename the variable
}
public:void print()
{
cout << "A szam : " <<tarol_var<< endl; // rename the variable
}
};

How to fix my program so that my functions can work? [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 7 years ago.
Improve this question
For HW (intro to C++) I had to modify a program that I wrote previously, I will be including that code here. Seeing the output of it will make things much more clearer:
//Program to compute and display the average and appropriate letter grade of 3 test scores
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
char grade;
double Test_1,Test_2,Test_3,Avg,ClassAvg1,ClassAvg2,ClassAvg3;
int sumTest_1,sumTest_2,sumTest_3;
int n;
sumTest_1=sumTest_2=sumTest_3=0;
const int totalSum=5.0;
for(n=1;n<=5;n++) {
do {
cout<<"What are the three test scores for student #"<<n;
cin>>Test_1>>Test_2>>Test_3;
if(Test_1<1||Test_1>100||Test_2<1||Test_2>100||Test_3<1||Test_3>100)
cout<<"You entered an invalid score - please try again"<<endl;
}
while(Test_1<1||Test_1>100||Test_2<1||Test_2>100||Test_3<1||Test_3>100);
Avg=((Test_1+Test_2+Test_3)/3.0);
if(Avg<65) {
grade= 'F';
}
else if(Avg<70)
{
grade= 'D';
}
else if(Avg<80)
{
grade= 'C';
}
else if(Avg<90)
{
grade= 'B';
}
else
{
grade='A';
}
cout<<setprecision (0)<<fixed;
cout<<"Your test average is "<<Avg<<" and your grade is " <<grade<<endl;
sumTest_1=sumTest_1+Test_1;
sumTest_2=sumTest_2+Test_2;
sumTest_3=sumTest_3+Test_3;
ClassAvg1=sumTest_1/5.0;
ClassAvg2=sumTest_2/5.0;
ClassAvg3=sumTest_3/5.0;
}
cout<<"The class average for test #1 is: "<<ClassAvg1<<endl;
cout<<"The class average for test #2 is: "<<ClassAvg2<<endl;
cout<<"The class average for test #3 is: "<<ClassAvg3<<endl;
}
For hw, I have to modify the program above by including two functions, ComputeAvg which will be called to compute and return the student's average and LetterGrade, to compute and return the letter grade. This is so that instead of using one main program, I now use these 2 functions. I have written this code so far, but I'm not sure if I'm doing it right and it will be great if someone can explain things out for me since I'm still not very comfortable writing functions:
//Program to compute and display the average and appropriate letter grade of 3 test scores
#include<iostream>
using namespace std;
void ComputeAvg(int Test_1, int Test_2, int Test_3)
{
double Avg;
double ComputeAvg(int Test_1,int Test_2,int Test_3);
{
Avg=ComputeAvg(Test_1,Test_2,Test_3);
return(Test_1+Test_2+Test_3)/3.0;
}
}
int main()
{
int Test_1,Test_2,Test_3,n;
double Avg;
for(n=1;n<=5;n++)
{
do
{
cout<<"What are the three test scores for student #"<<n;
cin>>Test_1>>Test_2>>Test_3;
if(Test_1<1||Test_1>100||Test_2<1||Test_2>100||Test_3<1||Test_3>100)
cout<<"You entered an invalid score - please try again"<<endl;
}
cout<<"Your test average is:"<<Avg<<endl;
ComputeAvg(Test_1,Test_2,Test_3);
}
}
#include<iostream>
using namespace std;
void LetterGrade(int Test_1, int Test_2, int Test_3)
{
double Avg;
double LetterGrade(int Test_1,int Test_2,int Test_3);
{
Grade=LetterGrade(Test_1,Test_2,Test_3);
void ComputeAvg(int Test_1, int Test_2, int Test_3)
{
double Avg;
double ComputeAvg(int Test_1,int Test_2,int Test_3);
{
Avg=ComputeAvg(Test_1,Test_2,Test_3);
return(Test_1+Test_2+Test_3)/3.0;
}
}
The above code doesn't look correct, especially using a nested function or recursion.
Something tells me you want a simplified version:
double Compute_Average(int test_1, int test_2, int test_3)
{
return ( static_cast<double>(test_1)
+ static_cast<double>(test_2)
+ static_cast<double>(test_3))
/ 3.0;
}
Since the function is returning a value, in your main function you want to store the result in a variable:
double Avg = Compute_Average(Test_1, Test_2, Test_3);
cout << "Test average is: " << Avg << "\n";

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

Cannot create 2 functions outside main [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'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;
}