Friday, July 31, 2009

I need some help in C++ to get this code compiled?

#include %26lt;iostream%26gt;





#include %26lt;cstdlib%26gt;


#include %26lt;ctime%26gt;


using namespace std;











int random_1();











int main()











{





int i = 0;





int j = 0;





int check;





int check_1;





char play_again = '\n' ;











int tortoise[70];





int hare[70];











cout %26lt;%26lt; "BANG !!!!!" %26lt;%26lt; endl;





cout %26lt;%26lt; "AND THEY'RE OFF !!!!!" %26lt;%26lt; endl;

















while(play_again == '\n')





{











while(i %26lt;= 69 %26amp;%26amp; j %26lt;= 69)





{











tortoise[i] = 0;;





check = random_1();











if(check == 1 || check == 2 || check == 3 || check == 4 || check ==


5)





tortoise[i+=3] = 0;











if(check == 6 || check == 7)





tortoise[i-=6] = 0;











if(check == 8 || check == 9 || check == 10)





tortoise[i+=1] = 0;











char *ptr;





char line[71] = "---------------------------------------...











ptr = line;











if(i %26lt; 0){





i = 0;





*(ptr + i) = 'T';}











*(ptr + i) = 'T';











hare[j] = 0;





check_1 = random_1();











if(check_1 == 1 || check_1 == 2)





hare[j] = 0;











if(check_1 == 3 || check_1 == 4)





hare[j+=9] = 0;











if(check_1 == 5)





hare[j+=12] = 0;











if(check_1 == 6 || check_1 == 7 || check_1 == 8)





hare[j++] = 0;











if(check_1 == 9 || check_1 == 10)





hare[j-=2] = 0;











if(j %26lt; 0){





j = 0;





*(ptr + j) = 'H';}











*(ptr + j) = 'H';











if(i == j){





*(ptr + i) = 'O';





*(ptr + i + 1) = 'U';





*(ptr + i + 2) = 'C';





*(ptr + i + 3) = 'H';}











cout %26lt;%26lt; "%s\n\n" %26lt;%26lt; endl;











break;











}

















if(i %26gt;= 69)





{





cout %26lt;%26lt; "TORTOISE WINS!!! YAY!!!" %26lt;%26lt; endl;





break;





}











if(j %26gt;= 69)





{





cout %26lt;%26lt; "HARE WINS. YUCH." %26lt;%26lt; endl;





break;





}











if(j %26gt;= 69 %26amp;%26amp; i %26gt;= 69)





{





cout %26lt;%26lt; "IT'S A TIE" %26lt;%26lt; endl;





break;





}











cout %26lt;%26lt; "Press enter" %26lt;%26lt; endl;





cin %26gt;%26gt; "%c",%26gt;%26gt; %26amp;play_again); // Error Here











}











return 0;

















}





int random_1()











{





srand(time(NULL));





return (1 + rand() % 10);





}

I need some help in C++ to get this code compiled?
Change the problem line to this:





cin %26gt;%26gt; play_again;
Reply:Iam studing 11-12th now.


so iam studying now about c++.


i wiil ans later.


sorry
Reply:would be better if you tell us the compile error you're receiving.
Reply:The previous answer was correct in the fix for your cin syntax, but this isn't going to do what you want. The newline won't be stored in play_again. Test your code, and you'll see how it works, or doesn't.





Something like this will do the job for you:





string s;


while (s.length() == 0) {


// ...


cout %26lt;%26lt; "Press return to continue, or x to exit ... ";


getline(cin,s);


}


No comments:

Post a Comment