--EDIT--
Sorry about that andyhunter ;)
Printable View
--EDIT--
Sorry about that andyhunter ;)
Kleid-0 - sshhh
See, we agree on stuff.Quote:
won't post any code for that project, but it's a typical learning tool.
Hint. Search your textbook for binary search method.
Sorry, I think you misunderstood and thought that comment was directed toward you. It wasn't. The distinction I made about C and C++ was directed to when the OP was receiving advice to mix C code within his C++ code (ie. inlcuding stdio.h and using the getchar function), and then advised to defend poor design to the instructor to boot.Quote:
Originally Posted by andyhunter
I then posted my code to show that you and I were on the same page :cool:
Couldn't find the Binary Search Method in my textbook...
Anyway, I started it...
What would I put where the question marks are, to end the program?Code:#include <iostream.h>
main()
int guess, ans, ans2;
int high = 100;
int low = 1;
cout << "Think of a number between 1 and 100\n";
cout << "My first guess is 50, is your number higher, lower, or 50?";
cout << "\n(higher = 1, lower = 2, correct guess = 0)";
cin >> ans;
if (ans = 1)
low = 50;
cout << "My guess is 75, is your number higher, lower, or 75?";
cout << "\n(higher = 1, lower = 2, correct guess = 0)";
if (ans2 = 1)
high = 100;
low = 76;
cout << "My guess is 88, is your number higher, lower, or 88?";
cout << "\n(higher = 1, lower = 2, correct guess = 0)";
else
if (ans2 = 2)
high = 74;
low = 51;
cout << "My guess is 62, is your number higher, lower, or 62?";
cout << "\n(higher = 1, lower = 2, correct guess = 0)";
if (ans2 = 0)
???????????
???????????
I'm probably starting this wrong anyway. So maybe a few suggestions would help. I started with, if the user entered 1, as in the number is greater than 50.
Dude, have you been sleeping throught this class?Quote:
Actually, I'm in C++ 1, and there will be no C++ 2 next year. So I will not learn more. And it's only a half a year class, so I'm finished with the class in about a week.
edit:
Ok here is some psuedocode:
This should help.Code:Set guessnumber = 0
Set a "found" flag to false
Set lower index to min number (your case 0)
Set higher number to max number(your case 100)
while found is false
guess = (max + min) / 2
Ask if lower or higher
if higher then
min = guess
if lower than
max = guess
if guess = number then
found = true
guessnumber = guessnumber + 1
loop
Print I guessed your number in guessnumber tries
No, there's no room to sleep on the desk with the computer there and all.
Look above I edited my post.
Ahhhhh. Let me try this out.
What do you mean by "found", never done that before :x
Just an integer variable indicating a true (1) or false (0) condition to be used in the while loop.
And remember pseudo doesn't exactly have to deal with the language's symantics.Code:bool found = false;
Blah!!! I was sure I got it this time. Anyway, I need help again.
I'm not sure if it's just crashing, or it's closing again. But I tried the getchar();getchar(); and that didn't stop it from closing, so I don't know.
For those who don't know, The objective of the program was for the user to think of a number between 1 and 100 and the program tries to guess what number it is.Code:#include <iostream.h>
main()
{
int max = 100;
int min = 1;
int guess, ans;
cout << "Think of a number between 1 and 100 and I will try to guess it\n";
do
{
guess = (max+min) / 2;
cout << "My guess is " << guess << ", is your number higher,lower, or 50?\n";
cout << "(higher = 1, lower = 0, correct guess = 2)";
if (ans = 1)
min = guess;
if (ans = 0)
max = guess;
if (ans = 2);
}
while (ans != 2)
cout << "I guessed your number!";
return 0;
}
edit:
Well, I corrected myself, It works good now.Code:#include <iostream.h>
#include <stdio.h>
main()
{
int max = 100;
int min = 1;
int guess, ans;
cout << "Think of a number between 1 and 100 and I will try to guess it\n";
do
{
guess = (max+min) / 2;
cout << "My guess is " << guess << " is your number higher,lower, or 50?\n";
cout << "(higher = 1, lower = 0, correct guess = 2)";
cin >> ans;
if (ans = 1)
min = guess;
else
if (ans = 0)
max = guess;
else
if (ans = 2)
}
while (ans != 2);
cout << "I guessed your number!";
getchar();getchar();
return 0;
}
P.S. thanks for the pseudo code!
edit edit: Actually It's still messed up, 0 and 2 don't work, no matter what I press it keeps going as if I went higher.
I see this in your code
It will never make it past the first "if" because you are assigning "=" 1 to ans so it will always be true. I think you want to compare "==" to 1 so that would be this.Code:if (ans = 1)
I only picked the first one you have two more to look at.Code:if (ans == 1)
Code:else
if (ans = 0)
max = guess;
else
if (ans = 2)
Ahhhhhhhhh! My teacher would kill me for that. Woops! Thanks man, that worked :p.
Here's one solution I wrote a while ago:
The comments, variable names and money is in Swedish, so try to figure the code out as an excercise. :)
Code:int main()
{
using namespace std;
//De här fälten innehåller de sedlar och namn på sedlar vi använder
//Endast dessa behöver ändras vid en enventuell införsel av Euro
const int valorer[] = {500,100,50,20,10,5,1,0};
const char* valorNamn[] = {"Femhundralappar","Hundralappar","Femtiolappar","Tjugolappar","Tiokronor",
"Femkronor","Enkronor",0};
//Hämta input
int belopp, betalat;
cout << "Skriv in belopp: ";
cin >> belopp;
cout << "Skriv in hur mycket du betalar med: ";
cin >> betalat;
//Beräkna växel
int tillbaka = betalat - belopp;
cout << "Tillbaka " << tillbaka << " kronor" << endl;
//Gå igenom listan med de olika valörerna
const int* valor = valorer;
const char** namn = valorNamn;
while (*valor && *namn)
{
//Så här många av denna typ ges tillbaka
const int antal = tillbaka / *valor;
//Minska summan med summan vi nu ger tillbaka
tillbaka = tillbaka % *valor;
//Visa att vi ger tillbaka
cout << *namn << " : " << antal << endl;
++valor;
++namn;
}
}
It's like you're speaking 3 different languages!