Thread: return problem

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    16

    return problem

    Hi!
    .........................
    if(...)
    .....
    else if(...)
    .......
    else
    return a string.

    after compiling and running the print out is=(AAAAAAAA8.00)

    How can my program print out the String (AAAAAAAA) without 8.00

    Code:
    #include<stdio.h>
    double my_program(double A, double B);
    int main(){
      double C=20;
      double D=2;
      printf("%.2f\n", my_program(C,D));
      return 0;
    }
    double my_program(double A, double B){
    
      double new_v;
       if(A<=2.0)
      {
        return B;
      }
      else if(A >2 && A<15)
      {
        new_v=(2+(A*3));
        return new_v;
    	}
      else
    	  return printf("AAAAAAAA");
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your program doesn't return a string. It returns whatever value printf returns. You are calling the printf function, so it does its job, and then it returns a value, which you in turn also return.

    Furthermore, you're using your function's return value in another printf statement, so that's always going to print whatever value you're giving it.

    You need to reread the portion of whatever book you're using on returning variables from functions, and on calling functions, because you don't seem to be clear on how they work too well.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    This will work
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    double my_program(double A, double B);
    int main(){
      double C=20;
      double D=2;
      printf("%.2f\n", my_program(C,D));
      system("pause");
      return 0;
    }
    double my_program(double A, double B){
    
      double new_v;
       if(A<=2.0)
      {
        return B;
      }
      else if(A >2 && A<15)
      {
        new_v=(2+(A*3));
        return new_v;
    	}
      else
    	  {
               printf("AAAAAAAA");
               system("pause");
               exit(0);
          }
    }

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    16
    Hi!
    Thank you for your help.
    peter

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  4. need help program crashing
    By tunerfreak in forum C++ Programming
    Replies: 14
    Last Post: 05-22-2006, 11:29 AM
  5. Alegro closes out on me
    By campsoup1988 in forum C++ Programming
    Replies: 8
    Last Post: 04-03-2006, 10:40 AM