Function call not working

This is a discussion on Function call not working within the C++ Programming forums, part of the General Programming Boards category; Why isn't this function call working? time = CalculateTime(service); Code: //******************************************************************// //Program Queue client file // // // // // ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    84

    Function call not working

    Why isn't this function call working?
    time = CalculateTime(service);


    Code:
    //******************************************************************//
    //Program Queue client file                                         //
    //                               //
    //  //
    //   //
    //                                                               //
    //                                                                  //
    //                                                                  //
    //******************************************************************//
    
    #include<iostream>
    #include<iomanip>
    #include<fstream>
    #include <cctype>   // defines isalpha() function
    #include<string>
    #include <time.h>
    #include <cstdlib>
    
    
    
    using namespace std;
    
    #include"QueueImple.cpp"
    
    int CalculateTime(int service);
    
    
    int main()
    {
    	queueList Lines;
        sortedList Words;
    	ifstream Infile;
    	ofstream Outfile;
    	Infile.open("inputQ.txt");
    	Outfile.open("OUTPUTQ.TXT");
    	int y = rand();
    	int x = rand();
    	int minutes = 720;
    	int Seed, arrive, service;
    	int customer = 0;
    int CustomerNumber=0;
    int runningtime;
    int time=0;
    
    srand(Seed);
    y = rand();
    x = rand();
    Infile>>customer;
    
    while(customer < 5)
    {
    
    
    srand(customer);
    arrive = rand()%100;  //First cutsomer arrival time
    service = rand()%100;     //First customer service time
    CustomerNumber++;
    Outfile<<"Customer "<<CustomerNumber<<endl;
    Outfile<<"Customer "<<CustomerNumber<<" arrival time "<<arrive<<endl;
    Outfile<<"Customer "<<CustomerNumber<<" service time "<<service<<endl;
    
    time = CalculateTime(service);
    
    Outfile<<time;
    Infile>>customer;
    }
    /*srand((unsigned)time(NULL)); //Casts time's return to unsigned
      int d=rand()%12;  //Should be more random now  
      Outfile<<d;
    */
    
        return 0;
    
    }
    
    int CalculateTime(int service)
    {
    int minutes= 720;
    int time;
    {
    	while(minutes > 0)
    
    	service++;
    minutes--;
    }
    time = service;
    return time;
    }

  2. #2
    Registered User
    Join Date
    Sep 2003
    Posts
    23
    Use indentation, so others will know what you mean.
    I think that problem is in curly brackets {} in CalculateTime() function. Anyway, that function seems strange. Why to use a loop, if you can do all the work by single statement
    Code:
    return (service + 720);
    ? Even why to use a function? (maybe for further development )

  3. #3
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    Code:
    int CalculateTime(int service)
    {
    int minutes= 720;
    int time;
    { //why do you have this brace here? 
    	while(minutes > 0)
    
    	service++;
    minutes--;
    }
    time = service;
    return time;
    }
    something like this will work:
    Code:
    int CalculateTime(int service)
    {
            int minutes= 720;
          
            while(minutes > 0){
                  service++;
                  minutes--;
            }
    
            return service;
    }
    I don't know why you would do that thought, since minutes = 720, what your while loop is doing is adding 720 to service.

    axon
    Last edited by axon; 09-18-2003 at 12:21 PM.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    here are some better ways (because I feel like writing some code):

    your code
    Code:
    int CalculateTime(int service)
    {
    int minutes= 720;
    int time;
    {
    	while(minutes > 0)
    
    	service++;
    minutes--;
    }
    time = service;
    return time;
    }
    for loop
    Code:
    int CalculateTime(int service)
    {
       int time;
    
           for(int minutes=720;minutes>0;minutes--)
              service++;
    
       time = service;
       return time;
    }
    without a loop
    Code:
    int CalculateTime(int service)
    {
       int time;
    
       service+=720;
    
       time = service;
       return time;
    }
    if you must use a function
    Code:
    int CalculateTime(int service) {return service+720;}
    what would make the most sense is to just not use a function...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get RSSI value, send to sensor, sensor receive package, repackage it?
    By techissue2008 in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-04-2009, 09:13 AM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. how to get function call stack
    By George2 in forum C Programming
    Replies: 18
    Last Post: 11-11-2006, 06:51 AM
  5. Problem with Template Function and overloaded equality operator
    By silk.odyssey in forum C++ Programming
    Replies: 7
    Last Post: 06-08-2004, 04:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21