Thread: Help convert C++ into C

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    1

    Help convert C++ into C

    hey everyone I have a C++ game code that i wrote to run rock paper, scissor,

    i need to convert it into C can anyone help me with this..

    Code:
    #include <iostream>
    using namespace std;
    
    
    #include <ctime>
    
    int main(void)
    {
    	srand((unsigned int) time(0));
    	int guess = 0, low = 0, high = 3, win = 0, lose = 0;
    	
    	cout << "\t\t\tRock Paper Siccors\n\n";
    	char choice = 0 ;
    	//round loop
    	for (int i =0; choice != 'q' && choice != 'Q'; i++)
    	{
    	guess = (rand() % (high - low +1)) +low;
    	cout << "Please choice one of the following:\n";
    	cout << "\t(R)ock\t(P)aper\t(S)cissors  (Q)uit\n";
    	cout << "Round " << i + 1 << endl;
    	cin.get(choice);
    	while(cin.get() != '\n'){};
    	switch(choice)
    	{
    	case 'r': case 'R':
    		cout << "You choose Rock.\n";
    		if (guess == 1)
    		{
    			cout << "The computer choose Rock, its a tie!\n\n";
    		}
    		else if (guess == 2)
    		{
    			cout << "The computer choose Paper, you lose!\n\n";
    			++lose;
    		}
    		else
    		{
    			cout << "The computer choose Scissors, you win!\n\n";
    			++win;
    		}
    		cout <<"The current score:\tWins: " << win << "  Loses: " << lose << endl;
    		system("pause");
    		system("cls");
    	break;
    
    	case 'p': case 'P':
    		cout << "You choose Paper.\n";
    		if (guess == 1)
    		{
    			cout << "The computer choose Rock, you win!\n\n";
    			++win;
    		}
    		else if (guess == 2)
    		{
    			cout << "The computer choose Paper, its a tie!\n\n";
    		}
    		else
    		{
    			cout << "The computer choose Scissors, you lose!\n\n";
    			++lose;
    		}
    		cout <<"The current score:\tWins: " << win << "  Loses: " << lose << endl;
    		system("pause");
    		system("cls");
    	break;
    
    	case 's': case 'S':
    		cout << "You choose Scissors.\n";
    		if (guess == 1)
    		{
    			cout << "The computer choose Rock, you lose!\n\n";
    			++lose;
    		}
    		else if (guess == 2)
    		{
    			cout << "The computer choose Paper, you win!\n\n";
    			++win;
    		}
    		else
    		{
    			cout << "The computer choose Scissors, its a tie!\n\n";
    		}
    
    		cout <<"The current score:\tWins: " << win << "  Loses: " << lose << endl;
    		system("pause");
    		system("cls");
    	break;
    
    	case 'q': case 'Q':
    		cout << "You played through " << i + 1 << " Round(s)\n";
    		cout <<"The final scores:\tWins: " << win << "  Loses: " << lose << endl;
    	break;
    
    	default:
    		cout << "Please choose an avalible selection.\n";
    	break;
    	}
    	}
    	return 0;
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    #include <iostream>
    using namespace std;
    
    
    #include <ctime>
    Replace all of that with:
    Code:
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    Then replace all the cout related stuff with printf/puts calls and the cin stuff with something like fgetc/getc/getchar. Also move any variable declarations before other bits of code, i.e this:
    Code:
    srand((unsigned int) time(0));
    int guess = 0, low = 0, high = 3, win = 0, lose = 0;
    	
    cout << "\t\t\tRock Paper Siccors\n\n";
    char choice = 0 ;
    has the variables choice an all those ints after srand and the cout. In C, those variable declarations would all need to be at the top of the main function before any call to srand for example. The variable declaration for i would also need to be moved in with those instead of being right in the for loop.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Convert to title case
    By gertie in forum C Programming
    Replies: 18
    Last Post: 07-06-2008, 10:58 PM
  3. Replies: 3
    Last Post: 08-21-2006, 06:42 AM
  4. Convert Char to Int Function
    By drdroid in forum C++ Programming
    Replies: 9
    Last Post: 02-19-2003, 12:53 PM
  5. please help ... to convert date to string
    By mel in forum C Programming
    Replies: 1
    Last Post: 06-12-2002, 10:26 AM