Thread: C++ to C

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

    Wink thx

    Thank you!

    Ok, here's some more.

    1)
    Code:
    cout << caaBoard[iRow][iCol];
    Would be this right?

    Code:
    printf("&#37;d", caaBoard[iRow][iCol]);
    Doesn't look right.

    2) I keep getting:

    error C2065: 'EMove' : undeclared identifier Line - 34
    error C2146: syntax error : missing ';' before identifier 'cNextMove' Line - 34
    error C2065: 'true' : undeclared identifier Lines - 40
    error C2143: syntax error : missing ';' before 'type' Lines - 51
    error C2143: syntax error : missing ')' before 'type' Lines - 51

    And about another 64 errors, roughly the same. lol

    Should I post them? And should I keep posting these replies?

  2. #17
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ok... change your &#37;d to a %c, unless you want it to come out a number.

    And on line 8 add:

    Code:
    typedef enum EMove EMove;
    And before main add these two lines

    Code:
    #define true 1
    #define false 0

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Or add #include <stdbool.h>, which gives you true and false.

  4. #19
    Registered User
    Join Date
    Oct 2008
    Posts
    31

    Lightbulb 11

    YAY! 11 errors left thanks to you guys.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #define true 1
    #define false 0
    
    int iRow, iCol, iIndex;
    
    /* Function declarations */
    void InitializeBoard(char[3][3]);
    void PrintBoard(char[3][3]);
    void LocateSpace(int *, int *, char [3][3]);
    void Randomize(char[3][3]);
    void Move(char[3][3], const EMove);
    
    enum EMove {            Up = 'w',
    			Down = 'z',
    			Left = 'a',
    			Right = 's'};
    
    int main() {
    	char caaBoard[3][3];
    	InitializeBoard(caaBoard);
    	Randomize(caaBoard);
    	
    	do {
    		char cNextMove;
    		typedef enum EMove EMove;
    		enum EMove eNextMove = (EMove)cNextMove;
    		Move(caaBoard, eNextMove);
    		PrintBoard(caaBoard);
    		printf("\nMove? ");
    		scanf("%c", &cNextMove);
    		printf("\n");
    	} while (true);
    
    	return EXIT_SUCCESS;
    }
    
    void InitializeBoard(char caaBoard[3][3]) {
    	const char kcaaInitial[3][3] = {
    			{'1', '2', '3'},
    			{'4', '5', '6'},
    			{'7', '8', '_'}
    	};
    	for (iRow = 0; iRow < 3; ++iRow) {
    		for (iCol = 0; iCol < 3; ++iCol) {
    			caaBoard[iRow][iCol] = kcaaInitial[iRow][iCol];
    		}
    	}
    }
    
    void PrintBoard(char caaBoard[3][3]) {
    	for (iRow = 0; iRow < 3; ++iRow) {
    		for (iCol = 0; iCol < 3; ++iCol) {
    			printf("%d", caaBoard[iRow][iCol];
    		}
    		printf("\n");
    	}
    }
    
    void LocateSpace(int &irRow, int &irCol, char caaBoard[3][3]) {
    	for (iRow = 0; iRow < 3; ++iRow) {
    		for (iCol = 0; iCol < 3; ++iCol) {
    			if (caaBoard[iRow][iCol] == ' ') {
    				irRow = iRow;
    				irCol = iCol;
    			}
    		}
    	}
    }
    
    void Randomize(char caaBoard[3][3]) {
    	srand(1000);
    	for (iIndex = 0; iIndex < 1000000; ++iIndex) {
    		const int kiNextMove = (rand() % 3);
    		switch (kiNextMove) {
    			case 0:
    				{
    					Move(caaBoard, Up);
    					break;
    				}
    			case 1:
    				{
    					Move(caaBoard, Down);
    					break;
    				}
    			case 2:
    				{
    					Move(caaBoard, Left);
    					break;
    				}
    			case 3:
    				{
    					Move(caaBoard, Right);
    					break;
    				}
    		}
    	}
    }
    
    void Move(char caaBoard[3][3], const EMove keMove) {
    	int iRowSpace;
    	int iColSpace;
    	LocateSpace(iRowSpace, iColSpace, caaBoard);
    	int iRowMove(iRowSpace);
    	int iColMove(iColSpace);
    	switch (keMove) {
    		case Up:
    			{
    				iRowMove = iRowSpace + 1;
    				break;
    			}
    		case Down:
    			{
    				iRowMove = iRowSpace - 1;
    				break;
    			}
    		case Left:
    			{
    				iColMove = iColSpace + 1;
    				break;
    			}
    		case Right:
    			{
    				iColMove = iColSpace - 1;
    				break;
    			}
    	}
    	/* Make sure that the square to be moved is in bounds */
    	if (iRowMove >= 0 && iRowMove < 3 && iColMove >= 0 && iColMove < 3) {
    		caaBoard[iRowSpace][iColSpace] = caaBoard[iRowMove][iColMove];
    		caaBoard[iRowMove][iColMove] = ' ';
    	} 
    }
    errors:

    error C2143: syntax error : missing ')' before ';' 66
    error C2143: syntax error : missing ')' before '&' 72
    error C2143: syntax error : missing '{' before '&' 72
    error C2059: syntax error : '&' 72
    error C2059: syntax error : ')' 72
    error C2061: syntax error : identifier 'keMove' 112
    error C2059: syntax error : '}' 145
    error C2059: syntax error : ';' 112
    error C2059: syntax error : ')' 112
    error C2449: found '{' at file scope (missing function header?) 112
    error C2146: syntax error : missing ')' before identifier 'keMove' 112

    I'm stumped. Thanks!

  5. #20
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Code:
    void LocateSpace(int *irRow, int *irCol, char caaBoard[3][3]) {
    	for (iRow = 0; iRow < 3; ++iRow) {
    		for (iCol = 0; iCol < 3; ++(*iCol)) {
    			if (caaBoard[iRow][iCol] == ' ') {
    				*irRow = iRow;
    				*irCol = iCol;
    			}
    		}
    	}
    }
    Again I am going to tell you..... but I will make it easy enough for you to copy and paste this time.

    Code:
    typedef enum EMove {
    	Up = 'w',
    	Down = 'z',
    	Left = 'a',
    	Right = 's'
    } EMove;
    Last edited by master5001; 10-17-2008 at 04:15 PM.

  6. #21
    Registered User
    Join Date
    Oct 2008
    Posts
    31

    6

    6 more!

    Code:
    void Move(char caaBoard[3][3], const EMove keMove) {
    errors (from the above line and last line of entire code):

    Code:
    error C2146: syntax error : missing ')' before identifier 'keMove'		
    error C2061: syntax error : identifier 'keMove'		
    error C2059: syntax error : ';'		
    error C2059: syntax error : ')'		
    error C2449: found '{' at file scope (missing function header?)		
    error C2059: syntax error : '}'		Last line (no clue why)

  7. #22
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    ... dude

    I will not get frustrated. I will go into my happy place....


    Code:
    void Move(char caaBoard[3][3], const enum EMove keMove) {

  8. #23
    Registered User
    Join Date
    Oct 2008
    Posts
    31

    Red face yes

    Yes, master...

    Tell me, how many times a day do you do this? Help out people like me.

    I think you enjoy making me feel inferior. That's your perk isn't it? Sicko.

    Code:
    LocateSpace(iRowSpace, iColSpace, caaBoard);
    int iRowMove = iRowSpace;
    int iColMove = iColSpace;
    errors:

    error C2143: syntax error : missing ';' before 'type' Line - 2
    error C2143: syntax error : missing ';' before 'type' Line - 3

    warnings:

    warning C4047: 'function' : 'int *' differs in levels of indirection from 'int' Line - 1
    warning C4024: 'LocateSpace' : different types for formal and actual parameter 1 Line - 1
    warning C4047: 'function' : 'int *' differs in levels of indirection from 'int' Line - 1
    warning C4024: 'LocateSpace' : different types for formal and actual parameter 2 Line - 1

    lol

  9. #24
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You are famililar with pointers, yes? Since LocateSpace intends to change things, you must pass the parameters by address, not by value.

  10. #25
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by zyphirr View Post
    I think you enjoy making me feel inferior. That's your perk isn't it? Sicko.
    If your plan is to get into a software development position, this is just a taste of what awaits you. A lot of developers have a superiority complex; some are just better at hiding it.
    "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

  11. #26
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by tabstop View Post
    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".
    As you understand, ".\puzzle 1000" is the same as "puzzle 1000". This isn't linux, no need to do the former.
    However, a better way of doing this is:
    From the IDE, goto project options, select debugging and enter 1000 under command arguments.
    Then hit F7 to compile, F5 to run.
    I still think it would be more beneficiary to learn/use C++, though.
    And as a last thing:

    Pointers can be written as
    T* var (puts emphasis on type; indeed, var is of type T*)
    and
    T *var (puts emphases on that *var is T; syntactically, this is correct)
    More info: http://www.research.att.com/~bs/bs_faq2.html#whitespace

    Don't blindly use one. Pick one.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

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

    thx

    Thanks for clearing that up Elysia!

    Ok, this is kind of off-topic, maybe.

    So, I'm currently using MS Visual Studio C++ 2008 to write, compile, and test my C programs. (No C++ yet, just C) And I'm doing this on a Vista, if that matters at all.

    Anyways, I compile my code and it has no errors (but some warnings). However, when I try to run this on a Linux (Redhat) OS, it doesn't work. It also gives me a bunch of new errors when I compile it on the Linux machine using gcc.

    I'm don't know much about compilers, but I've been reading that different "environments", whether it is compilers or the whole IDE, can cause this.

    Is there a way to fix this? Is there a way to configure MS Visual Studio to have a Linux-like "environment"? Should I dual-boot Linux / use Cygwin / use Eclipse / something else?

    I don't know if this could be the reason, but could it be that I need to change some settings on Visual Studio for a more specific C environment. (I don't want to overlap settings with C++) If so, can someone give me a list of settings to change?

    Thanks in advance!

  13. #28
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you write standard-compatible code, it should work on all compilers. Unfortunately, reality isn't as simple as it sounds in theory.
    What you can do is post the error messages (or warnings) you get when you compile your code. We can help you eliminate and fix them.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #29
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Turn the VC++ Warning Level up to 4 and fix all the errors & warnings. Then if you still get errors in gcc, you know they're all new errors.

    Also, maybe gcc is complaining because you're using C99 features? I think you need to specify an option to gcc to make it accept C99 features. Either that or you still have some C++ in the code. Make sure VC++ is compiling it as C instead of C++; changing the file extension from .cpp to .c should work, but there's also a compiler option to "Compile As C".
    "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

  15. #30
    Registered User
    Join Date
    Oct 2008
    Posts
    31

    :)

    No need to post the code Elysia, because I've fixed the errors on the Linux machine.

    This isn't the first time I've had to do it.

    I was contemplating those other options because I want to make VS identical to Linux so I don't have to deal with these situations anymore.

    -------------------------------------------------------------

    Also, maybe gcc is complaining because you're using C99 features?
    Yes, it probably is. I don't know what the C99 features are; therefore, I don't know how remove those features.

    I have played around with the VS options as you mentioned, but is there like an 1-step option to change Visual Studio C++ to Visual Studio C? Or do I have to manually configure the options?

    Thanks again guys!

Popular pages Recent additions subscribe to a feed