Thread: Additions to my program

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    18

    Additions to my program

    I have tried on another site to get some input by did not get any help so I am going to try here. I have wrote this for my class and was wondering if there were any additions you could suggest me to improve the working of it.....Beginner here as you can prob tell by the difficulty of this code. Thanks

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	cout <<"Thankyou for using CSE 1284's paint estimator. ";
    	
    	cout << "\n\nPlease enter following numbers in feet. ";
    
    	double roomWidth;
    	double roomLength;
    	double roomHeight;
    
    	cout << "\nHeight of the room:	";
    	cin >> roomHeight;
    	cout << "Width of the room:	";
    	cin >> roomWidth;
    	cout << "Length of the room:	";
    	cin >> roomLength;
    
    	double windowHeight;
    	double windowWidth;
    
    	cout << "Height of the window:	";
    	cin >> windowHeight;
    	cout << "Width of the window:	";
    	cin >> windowWidth;
    
    	double doorHeight;
    	double doorWidth;
    
    	cout << "Height of the door:	";
    	cin >> doorHeight;
    	cout << "Width of the door:	";
    	cin >> doorWidth; 
    	
    	double doorBoth = doorHeight * doorWidth;
    	double windowBoth = windowHeight * windowWidth;
    	double roomAll = roomWidth * roomHeight;
    	double roomA = roomHeight * roomLength;
    	double roomB = (roomAll + roomA) * 2;
    	double gallons = (roomB - (doorBoth + windowBoth)) / 350;
    	double coat;
    
    	cout <<"\nHow many coats of paint will be needed? " ;
    	cin >> coat;
    
    	double exactGallon = coat * gallons;
    
    	cout << "\n\n\n " <<exactGallon << " are needed to paint the room with " << coat << " coats. ";
    	cout << "\n\n ";
    
    
    	return 0;
    }

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You could prompt for the square foot coverage per gallon. You could prompt for primer. You could check for zeros. You could make all your messages constants and keep them together in one place. You could combine the getdoor and getwindow logic into a function you call, passing the text and returning the area.

    Nice job!

    Todd

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The probably biggest single flaw in the program is the lack of error checking. What if I enter "My room's height is 1000 meters!" instead of 1000. The program would not work at all
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    18
    Quote Originally Posted by Todd Burch View Post
    You could prompt for the square foot coverage per gallon. You could prompt for primer. You could check for zeros. You could make all your messages constants and keep them together in one place. You could combine the getdoor and getwindow logic into a function you call, passing the text and returning the area.

    Nice job!

    Todd
    thanks for the input. I'm starting to like this forum. Very quick reply's. I'm sure I will be on here for awhile as I learning c++ and taking the class right now and love it.

    Quote Originally Posted by Elysia View Post
    The probably biggest single flaw in the program is the lack of error checking. What if I enter "My room's height is 1000 meters!" instead of 1000. The program would not work at all
    yea your right.(I guess lol) I stated enter the number so hopefully if someone uses it they will enter just a number. I never tested it with a word but if I were to test it with a word what would I have to do for it to work? Or is it to complicated for me right now as I am just learning?

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by dukebdx12 View Post
    thanks for the input. I'm starting to like this forum. Very quick reply's. I'm sure I will be on here for awhile as I learning c++ and taking the class right now and love it.
    Yes, this forum is very active indeed . . . .

    yea your right.(I guess lol) I stated enter the number so hopefully if someone uses it they will enter just a number. I never tested it with a word but if I were to test it with a word what would I have to do for it to work? Or is it to complicated for me right now as I am just learning?
    You could try it. Your program will read 0 instead of a number, as will every successive call to cin. Since you don't check for zero, it's entirely possible that you'll get a divide-by-zero error.

    You can detect this very easily.
    Code:
    if(cin >> whatever) {
        // success
    }
    else {
        // failure
    }
    Of course, if you want to be very robust, when you detect a failure, you'd want to remove the offending characters from the input stream, and prompt the user for another number, perhaps after printing a stern message about arabic numerals.

    You can do this, too. http://faq.cprogramming.com/cgi-bin/...&id=1043284392

    And lastly, since you read so many numbers and things, I'd make a function out of it.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Nah. Another way to do it is to read a string and convert to a double using strod. You can also check if strtod returns 0 and the user didn't enter 0, then it's invalid data. It's not perfect, but it works.
    But it's also the "error factor" in that your hand may slip and you enter some letter by mistake!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by Elysia View Post
    Nah. Another way to do it is to read a string and convert to a double using strod. You can also check if strtod returns 0 and the user didn't enter 0, then it's invalid data. It's not perfect, but it works.
    You can also check, perfectly, if strtod() failed or not. If the end pointer is set to the same thing as the beginning pointer, it didn't read anything. http://linux.die.net/man/3/strtod

    See my example in this other thread: http://cboard.cprogramming.com/showp...3&postcount=11
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That's another way to do it, but it can read one, two, three characters and only half-succeed. So there's a lot of interpretation of like "1a" - is it valid? Should it ignore the "a"? And "1 " - is it valid?
    It would read one character and stop, not two. But generally you might think it would read and parse the entire string if it succeeds, but in that case, it wouldn't.

    So there's no 100&#37; guarantee method.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    18
    Quote Originally Posted by dwks View Post
    Yes, this forum is very active indeed . . . .


    You could try it. Your program will read 0 instead of a number, as will every successive call to cin. Since you don't check for zero, it's entirely possible that you'll get a divide-by-zero error.

    You can detect this very easily.
    Code:
    if(cin >> whatever) {
        // success
    }
    else {
        // failure
    }
    Of course, if you want to be very robust, when you detect a failure, you'd want to remove the offending characters from the input stream, and prompt the user for another number, perhaps after printing a stern message about arabic numerals.

    You can do this, too. http://faq.cprogramming.com/cgi-bin/...&id=1043284392

    And lastly, since you read so many numbers and things, I'd make a function out of it.
    got to remember i am very new at this and really only know a limited of things. I read the faq for invalid errors and understood most of the code but some things I havn't seen before in class and really don't know what to with and how to enter into my code. I'm not going to add it to my code just wanted to know what I can do better next time. Thanks

    - and I ran it and typed in feet with a number and it did not work. As in the program spit out numbers that are not right.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by Elysia View Post
    That's another way to do it, but it can read one, two, three characters and only half-succeed. So there's a lot of interpretation of like "1a" - is it valid? Should it ignore the "a"? And "1 " - is it valid?
    It would read one character and stop, not two. But generally you might think it would read and parse the entire string if it succeeds, but in that case, it wouldn't.

    So there's no 100% guarantee method.
    That's up to the programmer to decide. If you wanted to reject something like "1a", you could compare *end with '\0'. (Possibly after skipping over any whitespace.)

    - and I ran it and typed in feet with a number and it did not work. As in the program spit out numbers that are not right.
    Yeah, that's what will happen if you don't check for errors.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM