/*
Write a program that ccontinues to asks the user to enter any number other than 5 until the user enters the number 5.
Then tell the user "Hey! you weren't supposed to enter 5!" and exit the program.
★ Modify the program so that after 10 iterations if the user still hasn't entered 5 will tell the user "Wow, you're more patient then I am, you win." and exit.
★★ Modify the program so that it asks the user to enter any number other than the number equal to the number of times they've been asked to enter a number. (i.e on the first iteration
"Please enter any number other than 0" and on the second iteration "Please enter any number other than 1"m etc. etc. The program must behave accordingly exiting when the user enters the number they were asked not to.)
*/
#include <iostream>
#include "stdio.h"
#include "stdlib.h"
usingnamespace std;
int main()
{
int num1 = 0,num2 = 0;
do
{
num1 = num2;
cout << "\nEnter any number other than " << num1 <<": ";
cin >> num2;
cin.ignore();
if(num1 == num2)
{
cout << "\nHey! you weren't supposed to enter " << num2;
break;
}
else
{
}
}while(num1 != num2);
cin.get();
return 0;
}
I'm really enjoying the graduation program. It's actually the biggest C++ project I've tackled so far, and it's giving me a bit of insight into organizing a program. I just finished implementing the linked list, and I'm about to move onto the display elements of the program. I'll post some results when I'm done.
PopCOrn, I'm just going to tell you that you can make your program about 1/3 a size it is now and about 100 times less complex. For now, I'm not going to give a direct answer since you need to figure it out as a brain teaser :P
Computerquip, i really tried to figure it out before i posted =-\ but i really dont know how to. im currently studying alot again before i put my new knowlegde into work. also i believe it must be something with for, do while loops. ( i assume ).
i hope u could help me out here =) ( i will test ur code and then study off it again. so therefor even if u tell me the solution i still learn from it..
computerquip: I don't want to look at your code for the graduation problem lest I spoil some of it for myself, but how long is all of your code? I'm not done, but so far I feel like mine is too short, and I'd like to know how long a more experienced programmer's code ended up.
Well, how many lines is it? IIRC computerquip's was less than 400 LOC. I was going to do the graduation program but never did. Maybe I'll try it some time.
#include <iostream>
usingnamespace std;
int amount;
int x,a,b;
int intt (int q, int y);
int q,y,s;
int z,o;
void exit(){
system("pause");
}
void divide(){
do{
cout << o << endl;
o = o / 2;
} while ( o > 0);
}
void say_hello() {
cout << "How often do u want to print hello?" << endl;
cin >> amount;
do {
x++;
cout << "#: " << x << " Hello" << endl;
}while( x < amount);
system("pause");
}
int multiply(int q, int y)
{
int r;
r=q*y;
return (r);
}
int main(){
cout << "Hello Sir," << endl;
cout << "Lets start this shit " << endl;
cout << endl;
cout << "Hello please enter a number: ";
cin >> o;
if ( o>0){
divide();}
else
exit();
cout << "Please enter an integer" << endl;
cin >> q;
cout << "Please enter an integer" << endl;
cin >> y;
z = multiply (q,y);
cout << z << endl;
say_hello();
}
Mine was incomplete though and had some rather messy code involved. I quit around 3/4 of the way cuz of boredom. I'll probably start from scratch here soon since I don't even like the design I went with...
@ popcorn, the purpose of these challenges are to... er... challenge you. If you can't figure it out, Sleep on it and please do! I'll give you a decent hint to give you a boost.
If one person has the most, why can't he have the most until someone else beats him?
If you still have problems, ask again. I do want you to think about it though.
Also, don't use system("pause"). That doesn't work on my laptop since I'm running Linux. Use getchar() or something.
That's no reason to not use it, is it? It does the same thing and possibly requires fewer characters (depending on whether or not you use namespace std, although getchar() could require you to #include <cstdio>).
Computererquip, well all my thing do the thing they ask for, and trust me i tried to figure it out myself but after this tip ill probally be able to fix the pancake one =)
Not at all. In this case, a simple sort algorithm of the array would suffice. I'll show a homemade example later, I need to move some stuff to my new home.
Shortened explanation of what the algorithm does:
Given an array A, the program runs through A, comparing two elements at a time. If the two elements that the program compares at a time are in the wrong order, they are swapped, and the iterator advances by one. If they are in the right order, the iterator advances by one anyway. When the iterator reaches the end of A, the iterator is set back to the beginning of A and runs again. The program completes when no swaps are made in one of the passes (meaning the array is sorted).