any1 familar w/ C++?




Posted by thelaststand

alright so im trying to make my way through a C++ book, and they have exercises to do at the end of the chapters, and i cant figure how to do this one, it says:
"Write a program that asks the user to enter an hour value and a minute value. The main() function should then pass these two values to a type void function that displays the two values in the format shown inthe fallowing sample run:
Enter the number of hours: 9
Enter the number of minutes: 38
Time: 9:38"
the code i have gotten, after i've gotten some other people to try and help me, is this:
[code]
#include
using namespace std;
void hi(int a,int b);
int main()
{
int a; // here i tried int hi(a) too, but that didnt work either
int b; // here i tried int hi(b) too, but that didnt work either
cout << "enter a number in hours " << endl;
cin >> hi(a);
cout << "enter a number in minutes" << endl;
cin >> hi(b);
return 0;
}

void hi(int a,int b)
{
cout << "the time is " << a << ":" << b << endl;
}
[/code]
but when i try and compile that w/ g++ i get this:
[code]
Time.C: In function `int main()':
Time.C:6: error: `a' undeclared (first use this function)
Time.C:6: error: (Each undeclared identifier is reported only once for each
function it appears in.)
Time.C:7: error: `b' undeclared (first use this function)
sh-3.00$ vi Time.C
skipping 2 old session files
reading Time.C

sh-3.00$ g++ Time.C
Time.C: In function `int main()':
Time.C:6: error: `a' undeclared (first use this function)
Time.C:6: error: (Each undeclared identifier is reported only once for each
function it appears in.)
Time.C:7: error: `b' undeclared (first use this function)
[/code]
any1 know what i am doing wrong?




Posted by Arczu

Son of a... I can't remember C++ anymore. I took it a few years ago in highschool, but damn...

Had this been Java, I would be of more help. But if my memory serves me right, isn't the main method always a void?




Posted by WILLETH FOR MONTHS

As far as I can see, you need to declare your variables as global, rather than local, because you're calling a procedure. However, I've not dealt with void functions as of yet - I've only just started learning C++ - but that's what jumps out at me from your current code.




Posted by Wicked Sushi

[code]
#include
void simon(int);

using namespace std;

int main()
{
using namespace std;
simon(3);
cout << "Pck a number";
int count;
cin >> count;
simon(count);
cout << "Done" << endl;
return 0;
}
void simon(int n)
{
using namespace std;
cout << " Eat my shorts, pooper" << n << "times." << endl;
}
[/code]




Posted by thelaststand

how do u declare them as global (this exercise was at the end of chp. 2 so i know basically nothing)




Posted by WILLETH FOR MONTHS

AFAIK just declare them before the int main() line.




Posted by WILLETH FOR MONTHS

Right, I've done a bit more C++ now. AFAIK:

[code]#include
using namespace std;
int main()
void hi();
float a;
float b;
{
cout << "enter a number in hours :" << endl;
cin >> a;
cout << "enter a number in minutes :" << endl;
cin >> b;
hi();
return 0;
}

void hi()
{
cout << "the time is " << a << ":" << b << endl;
} [/code]

I'm pretty sure that's round about what's needed - although I know it's quite late. I may have confused where to put the ()'s as well.