please test random number guesing game program wouldnt stop printing string

I have random number guessing game, it works except for one lag - after input number it wouldnt stop printing "Cipars ir mazaks" or "cipars ir lielaks" (number is smaller, number is larger), it just prints ant prints, if i put break after cout text, it stops running programm completely ofcourse, maby someone can test this and its just an issue with my pc.

#include <cstdio>
#include <iostream>
#include <stdlib.h>
#include <ctime>
using namespace std;

int main ()

{
int a, b;

srand (time(0));

a = rand() % 10 + 1;

puts ("Uzmini ciparu no 1 to 10!");
cin>>b;
do {
if (a<b)
{puts ("Cipars ir mazaks");}
if (a>b)
{puts ("cipars ir lielaks");}
} while (a!=b);

puts ("Apsveicu!");
system("pause");
return 0;
}
Last edited on
closed account (NUj6URfi)
Hello, please use code tags.

Code cleanup:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <cstdio> 
 #include <iostream>
 #include <stdlib.h> 
 #include <ctime>
 using namespace std;

 int main () {
 int a, b;

 srand (time(0));

 a = rand() % 10 + 1;

 cout << "Uzmini ciparu no 1 to 10!"; //puts? Never heard of it. cout is standard print statement
 cin >> b;
 
do {

 if (a<b) {
cout <<  "Cipars ir mazaks";}
 if (a>b) {
cout << "cipars ir lielaks";}
 
} while (a!=b);

 cout << "Apsveicu!";
 cin.get(): // Never use system anything. It is slow and messy.
 return 0;
 }


This may fix your code. I do not know.
Last edited on
Thanks, i did write it like this before, i did find puts is the same as cin when using <cstdio>, tried everything, believe its an pc issue or im using dev-c++ and it acts on every pc im using differently although the same version, maby just should use different compiler
closed account (NUj6URfi)
Oh wait, found the error. Try:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <cstdio> 
 #include <iostream>
 #include <stdlib.h> 
 #include <ctime>
 using namespace std;

 int main () {
 int a, b;

 srand (time(0));

 a = rand() % 10 + 1;

 cout << "Uzmini ciparu no 1 to 10!"; 
cin >> b;
 
while (a ! b) {
 if (a<b) {
cout <<  "Cipars ir mazaks";}
 if (a>b) {
cout << "cipars ir lielaks";}
cin >> b
}

 cout << "Apsveicu!";
 cin.get():
 return 0;
 }
//solved it! This is how this works!
#include <cstdio>
#include <iostream>
#include <stdlib.h>
#include <ctime>
using namespace std;

int main ()
{
int a, b;
srand (time(0));
a = rand() % 10 + 1;

cout << "Guess the number from 1 to 10!"<<endl;
cin >> b;
do{
if (a<b) {cout << "Number is smaller"<<endl;cin >> b;}
else if (a>b) {cout << "Number is larger"<<endl;cin >> b;}
} while (a!=b);
cout << "Congrats!!"<<endl;
system("pause");
return 0;
}
Topic archived. No new replies allowed.