Thread: Return what??

  1. #1
    Unregistered
    Guest

    Return what??

    Help please for giving me any tips .....

    My problem is I dont know here what to return in the function defination.????
    This is the question.
    Write a funcn that displays a solid square of asterisks whose side is specified in integer parameter side.For eg.
    if side is 4 the funcn displays
    ****
    ****
    ****
    ****

    My code till now:

    int solid sq(int)
    int main()
    {
    int num;
    cout<<"enter how many sides"<<endl;
    cin>>num;
    cout<<solidsq(num)------------ ???//NOT SURE//
    return 0;
    }
    int soildsq(int x)
    {
    int counter=0;
    int count =0;
    for(counter=0;counter<=x;counter++)
    {
    for(count=0;count<=x;count++)
    {
    cout<<"*";
    }
    cout<<endl;
    }
    return ???????---------NOT AT ALL SURE???
    }

  2. #2
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385
    the general rule is return a zero if the program ran successfully and a non-zero if it failed or errored. Follow this unless you have a specific reason to return another value.
    Monday - what a way to spend a seventh of your life

  3. #3
    Unregistered
    Guest

    Re: Return what??

    Originally posted by Unregistered
    void solid sq(int);
    int main()
    {
    int num;
    cout<<"enter how many sides"<<endl;
    cin>>num;
    solidsq(num) // since the cout<< part is in the function there is no
    //need for it here
    return 0;
    }
    void soildsq(int x)//not too sure about making it void
    //but it doesn't look like you are returning anything
    {
    int counter=1;// having a 0 would make it have 5 *
    int count =1;// same as above
    for( ;counter<=x;counter++)//the first part has already
    //been intialized
    for( ;count<=x;count++)
    {
    cout<<"*";
    }
    cout<<endl;
    }
    }
    not sure if it will work because i don't have a compiler

  4. #4
    Unregistered
    Guest
    should be one { after that first for in the definition of your function

  5. #5
    Unregistered
    Guest

    Thank You ...But still

    Thanks unregister(s)

    But..there is a run time error,though it compiles perfectly,when I run the prog. given by u.

    Please please help, I am completely lost

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Code:
    #include <iostream>
    using namespace std;
    
    void solidsq(int x);
    
    int main() 
    { 
    int num; 
    cout<<"enter how many sides"<<endl; 
    cin>>num; 
    solidsq(num);
    
    return 0; 
    } 
    void solidsq(int x)
    { 
    int counter=1;
    int count =1;
    for( ;counter<=x;counter++){
    for( ;count<=x;count++){cout<<"*";}
    count = 1; // Put counter back to original value
    cout<<endl; }
    return;
    }
    That should compile.....well it does on mine....

  7. #7
    Unregistered
    Guest

    Smile thanks a bunch

    Thankx all of u for replying.
    Yup Fordy ,it worked now.
    Please tell me ,why u thought of reinitialising the count??
    I mean ,I am just new to this concept of programming.How did u think of that logic.And how to develop it....???
    I mean I feeling so dumb ..I thought why I cant think of such things..Please let me know ur secret

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  2. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 02:39 PM
  3. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  4. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM
  5. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 01:54 AM