Thread: returning a value with char

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    75

    returning a value with char

    I need some help with this program that I am writing.First of all in status, how do i return a number value with the char, and why does my program crash after a while...plus on build it gives me a warning abt all paths not returning a value in get status when it does...why???

    output:-
    /*
    Started Microwave, response is success
    Microwave in use. 90 seconds left
    Microwave not in use
    Started Microwave, response is success
    Microwave in use. 20 seconds left
    Microwave not in use
    Started Microwave, response is success
    Microwave in use. 40 seconds left
    Started Microwave, response is failure
    */

    the .h file
    Code:
    #include<time.h>
    class Microwave
    {
    public:
    	Microwave();
    	~Microwave();
    	bool start(double);
    	void stop(void);
    	char* status(void);
    
    private:
    	double stat;
    	double seconds;
    	time_t starts;
    	time_t end;
    	double diff;
    };
    .cpp file
    Code:
    #include "Microwave.h"
    #include<time.h>
    
    Microwave::Microwave()
    {
    	stat=0;
    	seconds=0;
    }
    
    bool Microwave::start(double set)
    {
    	if(stat!=NULL)return false;
    	
    	else
    	{
    	stat=set;
    	time(&starts);
    	return true;
    	}
    
    
    }
    
    void Microwave::stop(void)
    {
    	stat=0;
    	diff=0;
    
    }
    
    char* Microwave::status(void)
    {
    	time(&end);
    	
    	if(stat==0)
    	{
    		return "Microwave not in use";
    	}
    	
    	diff=difftime(end,starts);
    
    	if(diff<stat)
    	{
    		return "Microwave in use. <<m.stat-m.diff<< seconds left";
    	}
    
    }
    
    Microwave::~Microwave()
    {
    	stat=0;
    	
    		diff=0;
    }
    the main.cpp file
    Code:
    // Start takes the number of seconds to run.
    // Start will return a true if successful, false otherwise.
    
    // You can't start if it is already running.
    
    // Calling stop will stop the Microwave. Calling stop when it is not running has no effect.
    
    
    
    
    #include <iostream>
    #include <windows.h>
    #include "Microwave.h"
    
    using namespace std;
    
    void printStatus(Microwave& m)
    {
    	cout << m.status() << endl;
    }
    
    inline const char* printBool(bool result)
    {
    	return result ? "success" : "failure";
    }
    
    void main()
    {
    	Microwave aMicrowave;
    	bool result;
    
    	result = aMicrowave.start(90);
    	cout << "Started Microwave, response is " << printBool(result) << endl;
    	printStatus(aMicrowave);
    
    	Sleep(10000);
    	aMicrowave.stop();
    	printStatus(aMicrowave);
    
    	result = aMicrowave.start(30);
    	cout << "Started Microwave, response is " << printBool(result) << endl;
    	Sleep(10000);
    	printStatus(aMicrowave);
    
    	Sleep(21000);
    	printStatus(aMicrowave);
    
    	result = aMicrowave.start(40);
    	cout << "Started Microwave, response is " << printBool(result) << endl;
    	printStatus(aMicrowave);
    	Sleep(10000);
    
    	result = aMicrowave.start(10);
    	cout << "Started Microwave, response is " << printBool(result) << endl;
    }

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    >> First of all in status, how do i return a number value with the char

    You can't return multiple values. There are a couple ways of getting multiple pieces of information out though:

    1. Return an instance of a struct containing a number and a char*.
    2. Accept the number as an argument (non-const reference or a pointer), and change its value in the function.

    >> plus on build it gives me a warning abt all paths not returning a value in get status when it does
    Code:
    char* Microwave::status(void)
    {
    	time(&end);
    	
    	if(stat==0)
    	{
    		return "Microwave not in use";
    	}
    	
    	diff=difftime(end,starts);
    
    	if(diff<stat)
    	{
    		return "Microwave in use. <<m.stat-m.diff<< seconds left";
    	}
    
    }
    What does it return if both if statements evaluate to false (i.e. diff>stat)? Possibly also the source of a crash.

    Also, you're last return statement is a bit curious. It should do nothing more than print out that string, to place that value in a string, use something like sprintf.

    **edit**
    Rats! You beat me!
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    75
    some more tweaking i got it working, except for the last few lines, why does it spit out negative numbers at the end, and not look like the output requested.
    main.cpp, the first few lines are the output, my output is at the end.
    Code:
    /*
    Started Microwave, response is success
    Microwave in use. 90 seconds left
    Microwave not in use
    Started Microwave, response is success
    Microwave in use. 20 seconds left
    Microwave not in use
    Started Microwave, response is success
    Microwave in use. 40 seconds left
    Started Microwave, response is failure
    */
    
    #include <iostream>
    #include <windows.h>
    #include "Microwave.h"
    
    using namespace std;
    
    void printStatus(Microwave& m)
    {
    	cout << m.status() << endl;
    }
    
    inline const char* printBool(bool result)
    {
    	return result ? "success" : "failure";
    }
    
    void main()
    {
    	Microwave aMicrowave;
    	bool result;
    
    	result = aMicrowave.start(90);
    	cout << "Started Microwave, response is " << printBool(result) << endl;
    	printStatus(aMicrowave);
    
    	Sleep(10000);
    	aMicrowave.stop();
    	printStatus(aMicrowave);
    
    	result = aMicrowave.start(30);
    	cout << "Started Microwave, response is " << printBool(result) << endl;
    	Sleep(10000);
    	printStatus(aMicrowave);
    
    	Sleep(21000);
    	printStatus(aMicrowave);
    
    	result = aMicrowave.start(40);
    	cout << "Started Microwave, response is " << printBool(result) << endl;
    	printStatus(aMicrowave);
    	Sleep(10000);
    
    	result = aMicrowave.start(10);
    	cout << "Started Microwave, response is " << printBool(result) << endl;
    }
    .h file
    Code:
    #include<time.h>
    
    
    class Microwave
    {
    public:
    	Microwave();
    	~Microwave();
    	bool start(double);
    	void stop(void);
    	const char* status(void);
    
    private:
    	double seconds;
    	time_t begin;
    	time_t end;
    	double diff;
    };
    .cpp file
    Code:
    #include "Microwave.h"
    #include<time.h>
    #include<stdio.h>
    #include<string.h>
    
    Microwave::Microwave()
    {
    	seconds=0;
    	diff=0;
    	
    }
    
    Microwave::~Microwave()
    {
    	seconds=0;
    	diff=0;
    }
    
    bool Microwave::start(double times)
    {
    	if(seconds!=0)
    		return false;
    
    	seconds=0;
    	seconds=times;
    	time(&begin);
    	return true;
    }
    
    void Microwave::stop(void)
    {
    	seconds=0;
    	diff=0;
    }
    
    const char* Microwave::status(void)
    {
    	time(&end);
    	
    	if(seconds==0)
    		return "Microwave is not in use";
    	
    	if(seconds==difftime(end,begin))
    	{
    		seconds=0;
    		return "Microwave is not in use";
    	}
    
    	static char temp[80];
    	diff=difftime(end,begin);
    	sprintf(temp,"Microwave in use. %.0f seconds left",seconds-diff);
    	return temp;
    }
    my output:-
    Started Microwave, response is success
    Microwave in use. 90 seconds left
    Microwave is not in use
    Started Microwave, response is success
    Microwave in use. 20 seconds left //correct uptil here.
    Microwave in use. -1 seconds left
    Started Microwave, response is failure
    Microwave in use. -1 seconds left
    Started Microwave, response is failure

  4. #4
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    That looks a lot like Chapter 7 from C++ For Dummies. If you're reading that trying to learn C++, I strongly suggest that you find a better book.
    Away.

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    75
    its C++ How to Program, but any who any suggestions

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    75
    but still why does it give negative numbers, and the output not look anything like the original after some point?

    never mind i figured it out.
    Last edited by kashifk; 07-14-2003 at 08:10 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 08-11-2008, 11:02 PM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. returning char arrays!!!!
    By bobthebullet990 in forum C Programming
    Replies: 2
    Last Post: 03-30-2006, 07:05 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM