C++ to C

This is a discussion on C++ to C within the C Programming forums, part of the General Programming Boards category; Hello, I just need help converting this baby in plain old vanilla C. You can tell me the equivalents of ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    31

    C++ to C

    Hello, I just need help converting this baby in plain old vanilla C.

    You can tell me the equivalents of C to C++ or whatever is more convenient for you.

    I'm compiling in MS Visual Studio C++ 2008 and it gives me mostly "undeclared identifiers" and "syntax errors".

    Thanks in advance!

    Code:
    #include <iostream>
    #include <ctime>
    
    enum EMove {	keUp = 'w',
    			keDown = 'z',
    			keLeft = 'a',
    			keRight = 's'};
    
    // Function declarations
    void InitializeBoard(char[4][4]);
    void PrintBoard(char[4][4]);
    void LocateSpace(int&, int&, char [4][4]);
    void Randomize(char[4][4]);
    void Move(char[4][4], const EMove);
    
    int main() {
    	char caaBoard[4][4];
    	InitializeBoard(caaBoard);
    	Randomize(caaBoard);
    
    	using namespace std;
    	do {
    		PrintBoard(caaBoard);
    		cout << endl << "w = Up, z = Down, a = Left, s = Right" << endl;
    		char cNextMove;
    		cin >> cNextMove;
    		EMove eNextMove = (EMove)cNextMove;
    		Move(caaBoard, eNextMove);
    		cout << endl;
    	} while (true);
    
    	return EXIT_SUCCESS;
    }
    
    void InitializeBoard(char caaBoard[4][4]) {
    	const char kcaaInitial[4][4] = {
    			{'1', '2', '3', '4'},
    			{'5', '6', '7', '8'},
    			{'9', 'A', 'B', 'C'},
    			{'D', 'E', 'F', ' '}
    	};
    	for (int iRow = 0; iRow < 4; ++iRow) {
    		for (int iCol = 0; iCol < 4; ++iCol) {
    			caaBoard[iRow][iCol] = kcaaInitial[iRow][iCol];
    		}
    	}
    }
    
    void PrintBoard(char caaBoard[4][4]) {
    	using namespace std;
    	for (int iRow = 0; iRow < 4; ++iRow) {
    		for (int iCol = 0; iCol < 4; ++iCol) {
    			cout << caaBoard[iRow][iCol];
    		}
    		cout << endl;
    	}
    }
    
    void LocateSpace(int& irRow, int& irCol, char caaBoard[4][4]) {
    	for (int iRow = 0; iRow < 4; ++iRow) {
    		for (int iCol = 0; iCol < 4; ++iCol) {
    			if (caaBoard[iRow][iCol] == ' ') {
    				irRow = iRow;
    				irCol = iCol;
    			}
    		}
    	}
    }
    
    void Randomize(char caaBoard[4][4]) {
    	using namespace std;
    	srand((unsigned int)time(0));
    	for (int iIndex = 0; iIndex < 1000000; ++iIndex) {
    		const int kiNextMove = (rand() % 4);
    		switch (kiNextMove) {
    			case 0:
    				{
    					Move(caaBoard, keUp);
    					break;
    				}
    			case 1:
    				{
    					Move(caaBoard, keDown);
    					break;
    				}
    			case 2:
    				{
    					Move(caaBoard, keLeft);
    					break;
    				}
    			case 3:
    				{
    					Move(caaBoard, keRight);
    					break;
    				}
    		}
    	}
    }
    
    void Move(char caaBoard[4][4], const EMove keMove) {
    	int iRowSpace;
    	int iColSpace;
    	LocateSpace(iRowSpace, iColSpace, caaBoard);
    	int iRowMove(iRowSpace);
    	int iColMove(iColSpace);
    	switch (keMove) {
    		case keUp:
    			{
    				iRowMove = iRowSpace + 1;
    				break;
    			}
    		case keDown:
    			{
    				iRowMove = iRowSpace - 1;
    				break;
    			}
    		case keLeft:
    			{
    				iColMove = iColSpace + 1;
    				break;
    			}
    		case keRight:
    			{
    				iColMove = iColSpace - 1;
    				break;
    			}
    	}
    	// Make sure that the square to be moved is in bounds
    	if (iRowMove >= 0 && iRowMove < 4 && iColMove >= 0 && iColMove < 4) {
    		caaBoard[iRowSpace][iColSpace]	= caaBoard[iRowMove][iColMove];
    		caaBoard[iRowMove][iColMove]	= ' ';
    	} 
    }

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    printf()....

    Oh and include stdio.h instead of iostream. And time.h instead of ctime.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Also, C doesn't have references, so you'll have to convert those reference parameters to pointers.
    You also forgot to include <stdlib.h> for rand() & srand() (or <cstdlib> if you keep it as C++)
    You'll also have to get rid of any using namespace lines, and all variable declarations should be at the top of each block, before any other code (although I don't think that's required with C99).
    Just out of curiosity, why do you want to convert it to C?
    Last edited by cpjust; 10-17-2008 at 03:01 PM.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I gotta wonder about people like this, Salem. I mean he posted that question roughly the same time as he posted the question here and I gave my initial response 3 minutes after he asked the question... How impatient can one be?

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    31
    Just out of curiosity, why do you want to convert it to C?
    Actually no, you didn't answer his quesiton at all. Why does it need to be converted to C...
    Simple answer: I don't know how to code in C++.

    Motive: I'm trying to learn how to implement various puzzle algorithms to solve this puzzle.

    But first, I need the program with a functional puzzle to solve.

    After I get this converted, I need to modify it to randomize the numbers in the puzzle.

    Like you said, I need to use rand() and srand(), because I need to use a user input seed to randomize the puzzle.

    For example:

    The user inputs - ./puzzle 1000

    I need the 1000 to be the seed input that will generate the puzzle in a certain way, to which every time I input 1000 it will be the same arrangement.

    And if the user inputs a different seed, it will be a different arrangement.

    I hope I answered your question.

    Thanks again!

    I gotta wonder about people like this, Salem. I mean he posted that question roughly the same time as he posted the question here and I gave my initial response 3 minutes after he asked the question... How impatient can one be?
    I didn't know posting the same thread on multiple forums was against some standard forum posting code.

    Impatient? Yes, but I was expecting different answers from a different community (forum), and I want to see which forum I should post on more often base on the helpfulness and promptness of the responses in each forum.

    I hope that makes some sense.
    Last edited by zyphirr; 10-17-2008 at 03:29 PM. Reason: clarity

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Actually no, you didn't answer his quesiton at all. Why does it need to be converted to C...

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by master5001 View Post
    Actually no, you didn't answer his quesiton at all. Why does it need to be converted to C...
    Because he doesn't know how to program in C++. Although I'd say it would be a good thing to learn, since it has so many useful features to make coding easier.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    That is a good reason, I suppose. Though..... bah I don't want to disect this in a way I am not willing to staple back together.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by cpjust View Post
    Because he doesn't know how to program in C++. Although I'd say it would be a good thing to learn, since it has so many useful features to make coding easier.
    I wonder: does the OP know how to program in C either? After all, once you change cout for printf ... that's it. If he knows C, and wants to understand the algorithm, this "C++" program should present no difficulties at all.

    And had you read either forum for a week or so (or even a week's worth of posts) before posting, or just googled and jumped right in? How would you know what's standard operating procedure if you don't know anything about the community?

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I see tabstop is more willing to explore the avenue that I wasn't really willing to travel too deeply down. Why not just flow chart out your program?

  12. #12
    Registered User
    Join Date
    Oct 2008
    Posts
    31

    More question

    Ok, I just did some studying.

    1) Anyways, so this program uses a "time" header/class/something.

    Which defeats my purpose of generating pseudo-random numbers.

    So do I just remove it?

    2)
    Code:
      EMove eNextMove = (EMove)cNextMove;
    		Move(caaBoard, eNextMove);
    		PrintBoard(caaBoard);
    These 3 lines keep giving me "undeclared identifier" errors.

    I move them to the top of "do" loop, but it still does it.

    And I don't really understand them, the "enum" part that is.

    3) On a linux machine, I type in "./puzzle 1000" to execute the program with seed as 1000.

    How do I do that on MS Visual Studio C++ 2008?
    Last edited by zyphirr; 10-17-2008 at 03:53 PM. Reason: more

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by zyphirr View Post
    Ok, I just did some studying.

    1) Anyways, so this program uses a "time" header/class/something.

    Which defeats my purpose of generating pseudo-random numbers.

    So do I just remove it?
    Since you want the seed to come from the command line, you do not need time.h, that is correct.
    Quote Originally Posted by zyphirr View Post
    2)
    Code:
      EMove eNextMove = (EMove)cNextMove;
    		Move(caaBoard, eNextMove);
    		PrintBoard(caaBoard);
    These 3 lines keep giving me "undeclared identifier" errors.

    I move them to the top of "do" loop, but it still does it.

    And I don't really understand them, the "enum" part that is.
    enums are perfectly ordinary C that come up a fair bit. Unlike C++, though, you can't leave off the "enum", so these should be
    Code:
    enum EMove eNextMove = cNextMove;
    Quote Originally Posted by zyphirr View Post
    3) On a linux machine, I type in "./puzzle 1000" to execute the program with seed as 1000.

    How do I do that on MS Visual Studio C++ 2008?
    In Windows, the directory separator is \, not /, so you would need to type ".\puzzle 1000". However, usually the cwd is in the path, so you can just type "puzzle 1000".

  14. #14
    Registered User
    Join Date
    Oct 2008
    Posts
    31

    thx

    Thank you so much tabstop.

    To answer your question and master5001, honestly, I'm too lazy to write that whole program.

    I'm a beginner in C programming. And it's not as simple as cout to printf. I changed as much as I know in C to C++ equivalents.

    I just want to learn the algorithms of the puzzle, like A*, Breadth-first, Depth-first, etc., and maybe create my own solutions.

    Oh, and the remark about the forum:

    What do you mean "standard operating procedure"??

    I've joined daniweb like last year but wasn't an active poster. I just read stuff.

    And I recently joined here because "C Programming" sounds more of my cup of tea.
    Last edited by zyphirr; 10-17-2008 at 04:12 PM.

  15. #15
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well I say welcome, friend.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21