| | |
Efficient enough?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2008
Posts: 21
Reputation:
Solved Threads: 0
I had to write a program that asks the user to guess a number. The program then tells if their guess is too high or too low depending on the difference between their guess and the random number. I finished the program and it works, but my teacher's very picky about efficiency. The chapter is about controlled structures. Can someone look at this and tell me if there's a better way to write it? Mine's kinda long and doesn't seem right :\
Thanks for the help! =)
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { //declare the variables int num; //Variable to store the random number int guess; //Variable to store the number guessed by the user int isGuessed; //Boolean variable to control the loop int diff; //Variable to store the difference between the random number and guessed number int count; //Variable to count number of tries num = (rand() + time(0)) % 100; isGuessed = false; count = 0; while (!isGuessed) { while (count < 5) { cout << "Enter an integer greater than or equal to 0 and less than 100. You have 5 guesses: "; cin >> guess; cout << endl; diff = abs(num - guess); //Diff equals the difference between num and guess if (diff == 0) { cout << "You guessed the correct number." << endl; isGuessed = true; count = 5; } else if (diff >= 50) { if (guess > num) cout << "Your guess is very high!" << endl; else cout << "Your guess is very low!" << endl; } else if (diff >= 30) { if (guess > num) cout << "Your guess is high!" << endl; else cout << "Your guess is low!" << endl; } else if (diff >= 15) { if (guess > num) cout << "Your guess is moderately high!" << endl; else cout << "Your guess is moderately low!" << endl; } else { if (guess > num) cout << "Your guess is somewhat high!" << endl; else cout << "Your guess is somewhat low!" << endl; } count++; } //end while cout << "\nGame over!" << endl; if (isGuessed) cout << "You win!" << endl; else cout << "You lose!" << endl; isGuessed = true; } //end while return 0; }
Thanks for the help! =)
Last edited by ShadowOfBlood; Oct 10th, 2008 at 12:22 am.
Just browsing through your code. First thing, you don't need a nested while loop. Take out the outer while-loop, set the flag to true & break out of the loop when the answer is correct.
Also, I would use bool type for flag variables such as isGuessed.
Also, I would use bool type for flag variables such as isGuessed.
Founder of :
Lexel Technologies Pte Ltd - SMS (TXT) Marketing software solution
My Blogs: Gooner's Sanctuary
Pet Directory and Forum: FurryTale.net
Lexel Technologies Pte Ltd - SMS (TXT) Marketing software solution
My Blogs: Gooner's Sanctuary
Pet Directory and Forum: FurryTale.net
•
•
Join Date: Jan 2008
Posts: 3,828
Reputation:
Solved Threads: 501
I think you can get rid of the outer while loop:
Since you assign isGuessed to true at the end, you're only going to go through the loop once, right? So it's not really a loop. Also, and it's not really an efficiency issue, but it might be helpful to, instead of saying they have 5 guesses each time, tell them how many guesses they have remaining. That's a small issue though.
C++ Syntax (Toggle Plain Text)
while (!isGuessed) { // code isGuessed = true; } //end while
Since you assign isGuessed to true at the end, you're only going to go through the loop once, right? So it's not really a loop. Also, and it's not really an efficiency issue, but it might be helpful to, instead of saying they have 5 guesses each time, tell them how many guesses they have remaining. That's a small issue though.
>I finished the program and it works, but my teacher's very picky about efficiency.
Your program is I/O bound. The time it takes for a user to type a number and the time it takes to print the output will vastly overwhelm any processing efficiency. I recommend that you focus on clarity and correctness rather than efficiency.
Your program is I/O bound. The time it takes for a user to type a number and the time it takes to print the output will vastly overwhelm any processing efficiency. I recommend that you focus on clarity and correctness rather than efficiency.
I'm here to prove you wrong.
![]() |
Similar Threads
- Efficient Connection (Java)
- which one is Efficient????? (C)
- Help with efficient memory management (C)
- efficient lookup for vector<> (C)
- Logic to determine the efficient person (Legacy and Other Languages)
Other Threads in the C++ Forum
- Previous Thread: UML Solving...help pls
- Next Thread: how to connect C++ with Vb???
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






