If statements compiler problem
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
#include <iostream>
#include <string.h>
using namespace std;
double ConvertTocelcius (float fahrenheit);
unsigned int myStringLengthFunction(const char * str);
int main()
{
int choice;
float number, answer;
cout<<"Would you like to (1) Convert fahrenheit to Celcius or (2) calculate a word lenght? \n";
cout<<"Plase enter number 1 or 2 \n";
cin>>choice;
cout<<"please enter your number \n";
cin>>number;
if (choice ==1)
{
answer = ConvertTocelcius(float fahrenheit);
}
else
{
answer = myStringLengthFunction (const char * str);
}
cout<<"Your answer is" << answer << "\n";
cin.get();
cin.get();
return 0;
}
double ConvertTocelcius (float fahrenheit)
{
return (double)(fahrenheit - 32) * 5.0 / 9.0;
}
unsigned int myStringLengthFunction(const char * str)
{
return strlen(str);
}
|
i have two compiler errors 'expected primary expression before 'float' and 'const'
cant see what the problem is though??
line 22 and line 26 are the errors
i changed the lines to number, only one error now and thats with line 26,
error: cannot convert 'float' to 'const char*' for argument '1' to 'unsigned int myStringLengthFunction(const char*)'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
#include "5.01.h"
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
menu();
// cin.get();
cin.get();
return 0;
}
void menu()
{
int choice;
double ConvertToCelcius(float fahrenheit);
float Fahrenheit;
float answer;
double result = ConvertToCelcius(Fahrenheit);
char somechar;
cout<<"Please select a number \n";
cout<<"Number 1 = Convert Fahrenheit to Celsius \n";
//cout<<"Number 2 = Calculate a word lenght \n";
cout<<"Press 3 to Exit \n";
cin>>choice;
switch (choice)
{
case 1: //Fahrenheit
cout<<"Please enter a Fahrenheit \n";
cin>> Fahrenheit;
answer = result;
break;
//case 2: //string length
// string lenght here?????
case 3:
return;
default:
cout<<"wrong entry \n";
menu();
}
cout<<"The answer is" << answer <<"\n";
cout<<"Press enter/return key to continue \n";
cin>>somechar;
menu();
}
double ConvertToCelcius(float fahrenheit)
{
return (double)(fahrenheit - 32) * 5.0 / 9.0;
}
unsigned int myStringLengthFunction(const char * str)
{
return strlen(str);
}
|
tried another aproach,still having trouble with the string function, the 5.01a.h file is only a saved note pad with void main(); saved to it
really cant get either to work
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
#include "5.01.h"
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
menu();
//cin.get();
//cin.get();
return 0;
}
void menu()
{
int choice;
char somechar;
double ConvertToCelcius(float fahrenheit);
float answer;
float Fahrenheit;
double result = ConvertToCelcius(Fahrenheit);
unsigned int myStringLengthFunction(const char * str);
char anywordstring[100];
int mysize;
mysize=myStringLengthFunction(anywordstring);
cout<<"Please select a number \n";
cout<<"Number 1 = Convert Fahrenheit to Celsius \n";
cout<<"Number 2 = Calculate a word lenght \n";
cout<<"Press 3 to Exit \n";
cin>>choice;
switch (choice)
{
case 1: //Fahrenheit
cout<<"Please enter a Fahrenheit \n";
cin>> Fahrenheit;
answer = result;
break;
case 2: //string length
cout<<"Please enter a word \n";
cin.getline(anywordstring,100);
answer = mysize;
break;
case 3:
return;
default:
cout<<"wrong entry \n";
menu();
}
cout<<"The answer is" << answer <<"\n";
cout<<"Press enter/return key to continue \n";
cin>>somechar;
menu();
}
double ConvertToCelcius(float fahrenheit)
{
return (double)(fahrenheit - 32) * 5.0 / 9.0;
}
unsigned int myStringLengthFunction(const char * str)
{
return strlen(str);
}
|
this compiles ok, but when you pick option 2, it spits out the answer right away with a random number and/or letter
Topic archived. No new replies allowed.