### Beginner Exercises

Pages: 1... 1213141516
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546`` ``````/* 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 #include "stdio.h" #include "stdlib.h" using namespace 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; }``````
Hello, I am really new to C++ and i stumbled across this tread. and thought hey i could learn something here =)

the first couple project were pretty simple and easy but, the pancake one is giving me alot of trouble...

Currently im able to sort the number, but i can''t say who ate the most..
so please help me out :)

http://codepad.org/typHzJdB
Last edited on
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.
closed account (S6k9GNh0)
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.
I'm trying the Pancakes one, and I'm stuck already :(
I don't know how to find the highest number...suggestions?

 ``123456789101112131415161718192021222324252627`` ``````#include #include using namespace std; int person[5]; string amount = "How many pancakes did person"; int find () { } int main () { cout<< amount << " 1 eat?\n"; cin>> person[0]; cout<< amount << " 2 eat?\n"; cin>> person[1]; cout<< amount << " 3 eat?\n"; cin>> person[2]; cout<< amount << " 4 eat?\n"; cin>> person[3]; cout<< amount << " 5 eat?\n"; cin>> person[4]; return 0; }``````

Hello there again,

i did the fun with functions project so please give me feedback on how i can improve my coding.
=) try to not let me get used to bad habbits.

Probally to long for a simple program but it does the job =)

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970`` `````` #include using namespace 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(); }``````
closed account (S6k9GNh0)
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.
@computerquip,
We've been over this -- it's std::cin.get()! getchar() is a C function ;)
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>).

-Albatross
Last edited on
No, it doesn't make any difference AFAIK. But It's still technically incorrect. Consistency is good.
 it doesn't make any difference AFAIK. But It's still technically incorrect.
Which is it?
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 =)
closed account (S6k9GNh0)
SPOILER: http://codepad.org/60iarWV9

EDIT: Here's my version of it: http://codepad.org/miuCRAO8
Use a C style of bookmarking in the array to grab values you want...

This function reduces your code while still getting the greatest player.
Last edited on
And how do you get it to say it all in order?
like:

P1 ate 10
P2 ate 8
...
P10 ate 0

? Is it hard?
closed account (S6k9GNh0)
Oh, didn't even see that.

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.
Ok thanks computerquip :)

I didn't get there yet in C++ for dummies ...
http://en.wikipedia.org/wiki/Bubble_sort

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).

It's slow, but it's easy to implement.

-Albatross
Pages: 1... 1213141516