C++ question on void functions.  | |
February 12th, 2004, 12:11 AM
|
#1 (permalink)
| | Member
Join Date: Jun 2003 Location: Maine
Posts: 170
| C++ question on void functions.
I'm not sure if this is the right place but..
I have a void function thus:
void rowout (int columns)
{
int x(1);
while (x <= columns)
{
cout<<"*";
x += 1;
}
cout<<endl;
return;
}
This should print out a row of *s dependent on how many columns the user wants.
Now, back in the main function, I have another while loop that will tell how many times to makes these columns (it is the row loop and will make that line for how ever many rows)
Now, I'm trying to call this void function within the int main () while loop. I've tried putting cout<<rowout; but it gives me some hex stuff, and I can't figure out how to correctly invoke it.
Any help would be appreciated. |
| |
February 12th, 2004, 12:16 AM
|
#2 (permalink)
| | Space for Sale! :p
Join Date: Oct 2001 Location: La Isla del Encanto
Posts: 5,836
|
If I remember correctly, you need to call from main but you need this:
cout<<rowout(columns -- or whatever variable you are using);
So... you need to put the parenthesis of the function and the number that you are going to send to the void function.
__________________
boo!
|
| |
February 12th, 2004, 12:47 AM
|
#3 (permalink)
| | Ultimate Member
Join Date: Oct 2001 Location: Illinois
Posts: 2,977
|
so do you have something like: Code: int /*(or void)*/ main(){
int x=0,rows,columns;
cout<<"How many rows do you want?";
cin>>rows;
cout<<"How many columns do you want?";
cin>>columns;
while(x<rows){
rowout(columns);
x++;
}
return 0; //if you so please
} edit: forgot a ";" and I guess you don't need the "cout<<endl;" in the main if you have it in the rowout() already
edit 2- I forgot to add an "x++ in the loop" (stupid me)
Last edited by lost-and-found : February 12th, 2004 at 12:55 AM.
|
| |
February 12th, 2004, 12:56 AM
|
#4 (permalink)
| | Ultimate Member
Join Date: Oct 2001 Location: Illinois
Posts: 2,977
|
got this output when I mixed my main() with your rowout() is that what you wanted? Quote:
How many rows do you want?5
How many columns do you want?4
****
****
****
****
****
Press any key to continue
| edit- also, your function is void, you don't need a "return;" in the rowout() function, since you aren't returning anything.
Last edited by lost-and-found : February 12th, 2004 at 12:58 AM.
|
| |
February 12th, 2004, 01:01 AM
|
#5 (permalink)
| | Space for Sale! :p
Join Date: Oct 2001 Location: La Isla del Encanto
Posts: 5,836
|
Exactly what I meant! 
Nice example for the main. |
| |
February 12th, 2004, 05:22 PM
|
#6 (permalink)
| | Member
Join Date: Jun 2003 Location: Maine
Posts: 170
|
Thanks for the help, I got it working now.
I didn't realize that i didn't need to have the cout when calling the void function. |
| |
February 12th, 2004, 08:35 PM
|
#7 (permalink)
| | Ultimate Member
Join Date: Oct 2001 Location: Illinois
Posts: 2,977
|
do you mean like: "cout<<rowout(columns);"? well when you have the cout, it is expected that rowout() returns something then. That's why it gave you hex since there was nothing being returned. when you just have "rowout(columns);" the program knows all the work is done in the function rowout() and the program is not expecting rowout() to return any value. |
| | | Thread Tools | Search this Thread | | | | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |