Search Results

Search found 1848 results on 74 pages for 'printf'.

Page 5/74 | < Previous Page | 1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >

  • Printing out variables in c changes values of variables

    - by George Wilson
    I have an odd problem with some c-programme here. I was getting some wrong values in a matrix I was finding the determinant of and so I started printing variables - yet found that by printing values out the actual values in the code changed. I eventually narrowed it down to one specific printf statement - highlighted in the code below. If I comment out this line then I start getting incorrect values in my determinent calculations, yet by printing it out I get the value out I expect Code below: #include <math.h> #include <stdio.h> #include <stdlib.h> #define NUMBER 15 double determinant_calculation(int size, double array[NUMBER][NUMBER]); int main() { double array[NUMBER][NUMBER], determinant_value; int size; array[0][0]=1; array[0][1]=2; array[0][2]=3; array[1][0]=4; array[1][1]=5; array[1][2]=6; array[2][0]=7; array[2][1]=8; array[2][2]=10; size=3; determinant_value=determinant_calculation(size, array); printf("\n\n\n\n\n\nDeterminant value is %lf \n\n\n\n\n\n", determinant_value); return 0; } double determinant_calculation(int size, double array[NUMBER][NUMBER]) { double determinant_matrix[NUMBER][NUMBER], determinant_value; int x, y, count=0, sign=1, i, j; /*initialises the array*/ for (i=0; i<(NUMBER); i++) { for(j=0; j<(NUMBER); j++) { determinant_matrix[i][j]=0; } } /*does the re-cursion method*/ for (count=0; count<size; count++) { x=0; y=0; for (i=0; i<size; i++) { for(j=0; j<size; j++) { if (i!=0&&j!=count) { determinant_matrix[x][y]=array[i][j]; if (y<(size-2)) { y++; } else { y=0; x++; } } } } //commenting this for loop out changes the values of the code determinent prints -7 when commented out and -3 (expected) when included! for (i=0; i<size; i++) { for(j=0; j<size; j++){ printf("%lf ", determinant_matrix[i][j]); } printf("\n"); } if(size>2) { determinant_value+=sign*(array[0][count]*determinant_calculation(size-1 ,determinant_matrix)); } else { determinant_value+=sign*(array[0][count]*determinant_matrix[0][0]); } sign=-1*sign; } return (determinant_value); } I know its not the prettiest (or best way) of doing what I'm doing with this code but it's what I've been given - so can't make huge changes. I don't suppose anyone could explain why printing out the variables can actually change the values? or how to fix it because ideally i don't want to!!

    Read the article

  • unwanted quote marks in output from printf

    - by Gary
    I have some address data which has been exported from a database. If the address had multiple lines, the exported data has joined all the lines into one string with the former lines being separated by dollars signs. Here's one of the addresses: INFORMATION DELIVERY DEPT$704 CHERRY ST$ATLANTA, GA 30332-0900 I'm splitting this into an array on the dollar sign and outputting the three array elements into separate tab-separated fields with printf. For some reason, it comes out like this: INFORMATION DELIVERY DEPT 704 CHERRY ST "ATLANTA, GA 30332-0900" I don't want the quotes around that final field. Can anyone explain why this is happening and how to suppress it? This is being done in Windows Vista with gawk3.1.6. Thanks! gary

    Read the article

  • How to use a variable in an awk expression

    - by lugte098
    I'm trying to make this command: sed bla bla filename | awk '{printf "%s %s_entry_%.3f %.3f %.3f %.3f",$1,$3,$4,$5,$6,$7}' But the thing is, i want the %.3f part to be variable. So in one case it could be %.3f and in another it could be %.3f %.3f %.3f. So i'll just use a static one in my example code for clarity. So if i want 4 of these %.3f and put them in variable $values like so: values="%.3f %.3f %.3f %.3f" Then how can i put this string in the awk expression, without making awk to just put literally "${values}" in there. The following is my non-working-attempt: sed bla bla filename | awk '{printf "%s %s_entry_${values}",$1,$3,$4,$5,$6,$7}'

    Read the article

  • printf field width : bytes or chars?

    - by leonbloy
    The printf/fprintf/sprintf family supports a width field in its format specifier. I have a doubt for the case of (non-wide) char arrays arguments: Is the width field supposed to mean bytes or characters? What is the (correct-de facto) behaviour if the char array corresponds to (say) a raw UTF-8 string? (I know that normally I should use some wide char type, that's not the point) For example, in char s[] = "ni\xc3\xb1o"; // utf8 encoded "niño" fprintf(f,"%5s",s); Is that function supposed to try to ouput just 5 bytes (plain C chars) (and you take responsability of misalignments or other problems if two bytes results in a textual characters) ? Or is it supposed to try to compute the length of "textual characters" of the array? (decodifying it... according to the current locale?) (in the example, this would amount to find out that the string has 4 unicode chars, so it would add a space for padding).

    Read the article

  • How to catch printf from a dll?

    - by Xarx
    I've got a C++ console application that uses a third-party dll (jvm.dll, indirectly) that uses printf to print various error messages (Java stacktrace). I need to catch these stacktraces to a string in order to process them further, or at least to see them printed on the console. The thing is that I see the stacktrace only when debugging my application in VisualStudio (VS 2010). When I run my application in the "normal way", i.e. from the command line, nothing is printed on the console. So VS is able to somehow interfere the java output and display it. I need to be able to do the same thing. I've already tried freopen(), but without success. Also, I've found this question on the same problem, but without a clear answer.

    Read the article

  • How do you print a limited number of characters?

    - by Mike Pateras
    Sorry to put a post up about something so simple, but I don't see what I'm doing wrong here. char data[1024]; DWORD numRead; ReadFile(handle, data, 1024, &numRead, NULL); if (numRead > 0) printf(data, "%.5s"); My intention with the above is to read data from a file, and then only print out 5 characters. However, it prints out all 1024 characters, which is contrary to what I'm reading here. The goal, of course, is to do something like: printf(data, "%.*s", numRead); What am I doing wrong here?

    Read the article

  • Shell Screen -X Stuff problems

    - by user1621988
    OPTIONS="java -Xms1024M -Xmx1024M -jar craftbukkit.jar" PROCESS=server01 screen -dmS $PROCESS $OPTIONS nogui # Starting the application screen -x $PROCESS -X stuff `printf "stop\r"` # Closing the application screen -x $PROCESS # Attaching to the terminal of the application Starting the application work fine, however I got problems with stuff 'printf "stop/r"' It seems not to work when I just start up, wait some time and then try to Stop it with command above. But the strange thing is, that IF I did screen -x $PROCESS and detach (ctrl-A & ctrl-D) and then I use the Stop command it does work. So is there a way around to stuff printf without -screen -x the $PROCESS? Thank You in Advance

    Read the article

  • What's the best/most efficent way to create a semi-intelligent AI for a tic tac toe game?

    - by Link
    basically I am attempting to make a a efficient/smallish C game of Tic-Tac-Toe. I have implemented everything other then the AI for the computer so far. my squares are basically structs in an array with an assigned value based on the square. For example s[1].value = 1; therefore it's a x, and then a value of 3 would be a o. My question is whats the best way to create a semi-decent game playing AI for my tic-tac-toe game? I don't really want to use minimax, since It's not what I need. So how do I avoid a a lot of if statments and make it more efficient. Here is the rest of my code: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> struct state{ // defined int state; // 0 is tie, 1 is user loss, 2 is user win, 3 is ongoing game int moves; }; struct square{ // one square of the board int value; // 1 is x, 3 is o char sign; // no space used }; struct square s[9]; //set up the struct struct state gamestate = {0,0}; //nothing void setUpGame(){ // setup the game int i = 0; for(i = 0; i < 9; i++){ s[i].value = 0; s[i].sign = ' '; } gamestate.moves=0; printf("\nHi user! You're \"x\"! I'm \"o\"! Good Luck :)\n"); } void displayBoard(){// displays the game board printf("\n %c | %c | %c\n", s[6].sign, s[7].sign, s[8].sign); printf("-----------\n"); printf(" %c | %c | %c\n", s[3].sign, s[4].sign, s[5].sign); printf("-----------\n"); printf(" %c | %c | %c\n\n", s[0].sign, s[1].sign, s[2].sign); } void getHumanMove(){ // get move from human int i; while(1){ printf(">>:"); char line[255]; // input the move to play fgets(line, sizeof(line), stdin); while(sscanf(line, "%d", &i) != 1) { //1 match of defined specifier on input line printf("Sorry, that's not a valid move!\n"); fgets(line, sizeof(line), stdin); } if(s[i-1].value != 0){printf("Sorry, That moves already been taken!\n\n");continue;} break; } s[i-1].value = 1; s[i-1].sign = 'x'; gamestate.moves++; } int sum(int x, int y, int z){return(x*y*z);} void getCompMove(){ // get the move from the computer } void checkWinner(){ // check the winner int i; for(i = 6; i < 9; i++){ // check cols if((sum(s[i].value,s[i-3].value,s[i-6].value)) == 8){printf("The Winner is o!\n");gamestate.state=1;} if((sum(s[i].value,s[i-3].value,s[i-6].value)) == 1){printf("The Winner is x!\n");gamestate.state=2;} } for(i = 0; i < 7; i+=3){ // check rows if((sum(s[i].value,s[i+1].value,s[i+2].value)) == 8){printf("The Winner is o!\n");gamestate.state=1;} if((sum(s[i].value,s[i+1].value,s[i+2].value)) == 1){printf("The Winner is x!\n");gamestate.state=2;} } if((sum(s[0].value,s[4].value,s[8].value)) == 8){printf("The Winner is o!\n");gamestate.state=1;} if((sum(s[0].value,s[4].value,s[8].value)) == 1){printf("The Winner is x!\n");gamestate.state=2;} if((sum(s[2].value,s[4].value,s[6].value)) == 8){printf("The Winner is o!\n");gamestate.state=1;} if((sum(s[2].value,s[4].value,s[6].value)) == 1){printf("The Winner is x!\n");gamestate.state=2;} } void playGame(){ // start playing the game gamestate.state = 3; //set-up the gamestate srand(time(NULL)); int temp = (rand()%2) + 1; if(temp == 2){ // if two comp goes first temp = (rand()%2) + 1; if(temp == 2){ s[4].value = 2; s[4].sign = 'o'; gamestate.moves++; }else{ s[2].value = 2; s[2].sign = 'o'; gamestate.moves++; } } displayBoard(); while(gamestate.state == 3){ if(gamestate.moves<10); getHumanMove(); if(gamestate.moves<10); getCompMove(); checkWinner(); if(gamestate.state == 3 && gamestate.moves==9){ printf("The game is a tie :p\n"); break; } displayBoard(); } } int main(int argc, const char *argv[]){ printf("Welcome to Tic Tac Toe\nby The Elite Noob\nEnter 1-9 To play a move, standard numpad\n1 is bottom-left, 9 is top-right\n"); while(1){ // while game is being played printf("\nPress 1 to play a new game, or any other number to exit;\n>>:"); char line[255]; // input whether or not to play the game fgets(line, sizeof(line), stdin); int choice; // user's choice about playing or not while(sscanf(line, "%d", &choice) != 1) { //1 match of defined specifier on input line printf("Sorry, that's not a valid option!\n"); fgets(line, sizeof(line), stdin); } if(choice == 1){ setUpGame(); // set's up the game playGame(); // Play a Game }else {break;} // exit the application } printf("\nThank's For playing!\nHave a good Day!\n"); return 0; }

    Read the article

  • shift reduce&& reduce reduce errors in build parser for python garmmer

    - by user366580
    i wanna build buttom up parser by java cup i write code in java cup , it is for python language so i used grammer was written in this site : but not all grammer , i choice partial set ,just while , identifer also i smiplified them when i did compile for the java cup that i write by write this command in command prompt window : java java_cup.Main -parser CalcParser -symbols CalcSymbol < javacupfile.cup i get conflict errors ,they are of type reduce-shift conflict and reduce-reduce conflict you can see to print screen of the errors in these links image 1 click here to see imge1 the grammer was in EBNF form in as refernce site and i convert it to BNF form maybe i make mistake in converting so i get such errors the origanl grammmer was // grammer in EBNF form identifier ::= (letter|"_") (letter | digit | "_")* letter ::= lowercase | uppercase lowercase ::= "a"..."z" uppercase ::= "A"..."Z" digit ::= "0"..."9 compound_stmt ::= if_stmt | while_stmt for_stmt ::= "for" target_list "in" expression_list ":" suite ["else" ":" suite] while_stmt ::= "while" expression ":" suite ["else" ":" suite] suite ::= stmt_list NEWLINE stmt_list ::= simple_stmt (";" simple_stmt)* [";"] simple_stmt ::= expression_stmt expression_stmt ::= expression_list expression_list ::= expression ( "," expression )* [","] expression ::= conditional_expression conditional_expression ::= or_test ["if" or_test "else" expression] or_test ::= and_test | or_test "or" and_test and_test ::= not_test | and_test "and" not_test not_test ::= comparison | "not" not_test comparison ::= or_expr ( comp_operator or_expr )* comp_operator ::= "<" | ">" | "==" | ">=" | "<=" | "<>" | "!=" | "is" ["not"] | ["not"] "in" or_expr ::= xor_expr | or_expr "|" xor_expr xor_expr ::= and_expr | xor_expr "^" and_expr and_expr ::= "&" | and_expr the grammer after converting to BNF form identifier ::=letterletter| letterdigit| letter"_"| "_"letter | "_"digit | "_""_" letter ::= lowercase | uppercase lowercase ::= "a"..."z" uppercase ::= "A"..."Z" digit ::= "0"..."9 while_stmt ::= "while" expression ":" suite "else" ":" suite |"while" expression ":" suite suite ::= stmt_list NEWLINE stmt_list ::= simple_stmt ";" simple_stmt stmt_list|";" simple_stmt ::= expression_stmt expression_stmt ::= expression_list expression_list ::= expression "," expression expression_list| "," expression ::= conditional_expression conditional_expression ::= or_test "if" or_test "else" expression |or_test or_test ::= and_test | or_test "or" and_test and_test ::= not_test | and_test "and" not_test not_test ::= comparison | "not" not_test comparison ::= or_expr comp_operator or_expr comp_operator ::= "<" | ">" | "==" | ">=" | "<=" | "<>" | "!=" | "is" ["not"] | ["not"] "in" or_expr ::= xor_expr | or_expr "|" xor_expr xor_expr ::= and_expr | xor_expr "^" and_expr and_expr ::= "&" | and_expr and the java cup file that i compile and get those errors is import java.io.*; terminal COMA; terminal ELSE; terminal WHILE; terminal NEWLINE; terminal SEMCOLON; terminal CAMMA; terminal IF; terminal OR; terminal AND; terminal NOT; terminal LESS; terminal GREATER; terminal EQUAL; terminal GREATERorE; terminal LESSorE; terminal NEQUAL; terminal OROP; terminal XOROP; terminal ANDOP; terminal Integer DIGIT; terminal java.lang.String LOWERCASE; terminal java.lang.String UPPERCASE; non terminal java.lang.String IDENTIFIER; non terminal java.lang.String LETTER; non terminal COMPOUND_STMT; non terminal WHILE_STMT; non terminal EXPRESSION; non terminal SUITE ; non terminal STMT_LIST; non terminal SIMPLE_STMT; non terminal EXPRESSION_STMT; non terminal EXPRESSION_LIST; non terminal CONDITITONAL_EXPRESSION; non terminal OR_TEST; non terminal AND_TEST; non terminal NOT_TEST; non terminal COMPARISON; non terminal COMP_OPERATOR; non terminal OR_EXPR; non terminal XOR_EXPR; non terminal AND_EXPR; IDENTIFIER ::=LETTER{: System.out.printf("lowercase"); :}| {: System.out.printf("uppercase"); :} LETTER{: System.out.printf("lowercase"); :}| {: System.out.printf("uppercase"); :}| LETTER{: System.out.printf("lowercase"); :}| {: System.out.printf("uppercase"); :} DIGIT; LETTER ::= LOWERCASE | UPPERCASE; COMPOUND_STMT ::=WHILE_STMT; WHILE_STMT ::= WHILE{: System.out.printf( "while"); :} EXPRESSION COMA {: System.out.printf(":"); :} SUITE ELSE {: System.out.printf("else" ); :} COMA{: System.out.printf( ":" ); :} SUITE |WHILE{: System.out.printf( "while" ); :} EXPRESSION COMA{: System.out.printf( ":" ); :} SUITE; SUITE ::= STMT_LIST NEWLINE{: System.out.printf( "newline" ); :}; STMT_LIST ::= SIMPLE_STMT SEMCOLON{: System.out.printf( ";" ); :} SIMPLE_STMT STMT_LIST|SEMCOLON{: System.out.printf( ";" ); :}; SIMPLE_STMT ::=EXPRESSION_STMT; EXPRESSION_STMT ::=EXPRESSION_LIST; EXPRESSION_LIST ::= EXPRESSION CAMMA{: System.out.printf( "," ); :} EXPRESSION EXPRESSION_LIST| CAMMA{: System.out.printf( "," ); :}; EXPRESSION ::= CONDITITONAL_EXPRESSION; CONDITITONAL_EXPRESSION ::= OR_TEST IF{: System.out.printf( "if"); :} OR_TEST ELSE{: System.out.printf("else"); :} EXPRESSION |OR_TEST; OR_TEST ::= AND_TEST | OR_TEST OR{: System.out.printf( "or"); :} AND_TEST; AND_TEST ::= NOT_TEST | AND_TEST AND{: System.out.printf( "and"); :} NOT_TEST; NOT_TEST ::= COMPARISON | NOT{: System.out.printf("not"); :} NOT_TEST; COMPARISON ::= OR_EXPR COMP_OPERATOR OR_EXPR ; COMP_OPERATOR ::= LESS{: System.out.printf( "<"); :} | GREATER{: System.out.printf(">"); :} | EQUAL{: System.out.printf("=="); :} | GREATERorE{: System.out.printf(">="); :} | LESSorE{: System.out.printf("<="); :} | NEQUAL{: System.out.printf("!="); :}; OR_EXPR ::= XOR_EXPR | OR_EXPR OROP{: System.out.printf("|"); :} XOR_EXPR; XOR_EXPR ::= AND_EXPR | XOR_EXPR XOROP {: System.out.printf("^"); :}XOR_EXPR; AND_EXPR ::= ANDOP{: System.out.printf("&"); :} | AND_EXPR; can any one told me how can solve this errors to build parser correcrtly??

    Read the article

  • std::string.resize() and std::string.length()

    - by dreamlax
    I'm relatively new to C++ and I'm still getting to grips with the C++ Standard Library. To help transition from C, I want to format a std::string using printf-style formatters. I realise stringstream is a more type-safe approach, but I find myself finding printf-style much easier to read and deal with (at least, for the time being). This is my function: using namespace std; string formatStdString(const string &format, ...) { va_list va; string output; size_t needed; size_t used; va_start(va, format); needed = vsnprintf(&output[0], 0, format.c_str(), va); output.resize(needed + 1); // for null terminator?? used = vsnprintf(&output[0], output.capacity(), format.c_str(), va); // assert(used == needed); va_end(va); return output; } This works, kinda. A few things that I am not sure about are: Do I need to make room for a null terminator, or is this unnecessary? Is capacity() the right function to call here? I keep thinking length() would return 0 since the first character in the string is a '\0'. Occasionally while writing this string's contents to a socket (using its c_str() and length()), I have null bytes popping up on the receiving end, which is causing a bit of grief, but they seem to appear inconsistently. If I don't use this function at all, no null bytes appear.

    Read the article

  • implementation of text editor in shell programming

    - by Arka Ghosh
    i have made a text editor in C. when i am changing the extension of that file from .c to .sh and compiling the file in the terminal,some error is shown,like for the global variables an external error is shown,and for the functions i have declared errors are shown there also.please help me to solve this. I am sending my code.. include include include int i,j,ec,fg,ec2; char fn[20],e,c,d; FILE *fp1,*fp2,fp; void Create(); void Append(); void Delete(); void Display(); int main() { do { printf("\n\t\t** TEXT EDITOR *"); printf("\n\n\tMENU:\n\t..\n "); printf("\n\t1.CREATE\n\t2.DISPLAY\n\t3.APPEND\n\t4.DELETE\n\t5.EXIT\n"); printf("\n\tEnter your choice: "); scanf("%d",&ec); switch(ec) { case 1: Create(); break; case 2: Display(); break; case 3: Append(); break; case 4: Delete(); break; case 5: exit(1); } }while(1); } void Create() { fp1=fopen("temp.txt","w"); printf("\n\tEnter the text and press '.' to save\n\n\t"); while(1) { c=getchar(); fputc(c,fp1); if(c == '.') { fclose(fp1); printf("\n\tEnter then new filename: "); scanf("%s",fn); fp1=fopen("temp.txt","r"); fp2=fopen(fn,"w"); while(!feof(fp1)) { c=getc(fp1); putc(c,fp2); } fclose(fp2); break; }} } void Display() { printf("\n\tEnter the file name: "); scanf("%s",fn); fp1=fopen(fn,"r"); if(fp1==NULL) { printf("\n\tFile not found!"); goto end1; } while(!feof(fp1)) { c=getc(fp1); printf("%c",c); } end1: fclose(fp1); printf("\n\n\tPress any key to continue.."); } void Delete() { printf("\n\tEnter the file name: "); scanf("%s",fn); fp1=fopen(fn,"r"); if(fp1==NULL) { printf("\n\tFile not found!"); goto end2; } fclose(fp1); if(remove(fn)==0) { printf("\n\n\tFile has been deleted successfully!"); goto end2; } else printf("\n\tError!\n"); end2: printf("\n\n\tPress any key to continue.."); getchar(); } void Append() { printf("\n\tEnter the file name: "); scanf("%s",fn); fp1=fopen(fn,"r"); if(fp1==NULL) { printf("\n\tFile not found!"); goto end3; } while(!feof(fp1)) { c=getc(fp1); printf("%c",c); } fclose(fp1); printf("\n\tType the text and press 'Ctrl+s' to append.\n"); fp1=fopen(fn,"a"); while(1) { c=getchar(); if(c==19) goto end3; if(c==13) { d='\n'; fputc(d,fp1); } else { fputc(c,fp1); } } end3: fclose(fp1); }

    Read the article

  • sprintf in Delphi?

    - by kroimon
    Hi there! Does anyone know a 100% clone of the C/C++ printf for Delphi? Yes, I know the System.Format function, but it handles things a little different. For example if you want to format 3 to "003" you need "%03d" in C, but "%.3d" in Delphi. I have an application written in Delphi which has to be able to format numbers using C format strings, so do you know a snippet/library for that? Thanks in advance!

    Read the article

  • What primitive data type is time_t?

    - by thyrgle
    I do not know the data type of time_t. Is it a float double or something else? Because if I want to display it I need the tag that corresponds with it for printf. I can handle the rest from there for displaying time_t but I need to know the data type that corresponds with it.

    Read the article

  • Printing a string and variable in MIPS

    - by Matt
    Here's the C representation of what I'm trying to do in MIPS assembly: printf ("x=%d\n", x); I know that I can do a syscall to easily print x= and I can also do a syscall to print the int x (which is stored in a register). However, that prints them like this (let's say x is 5): x= 5 How can I make them print on the same line?

    Read the article

  • problem in printing floating point

    - by kudi
    hi I am using IAR c compiler, I am trying to print floating point value like printf("version number: %f\n",1.4); but I am always getting like below in console version number:ERROR help please thanks in advance kudi

    Read the article

  • C/C++ definitions of functions

    - by Vit
    Yesterday, I have been watching discussion here, about compilers and linkers. It was about C library function definitions. I have never thought of that, so it inspired me to do some searching, but I cannot find exactly what I want. I wonder, what is the smallest syntax you need to add into your source code to enable just printf() function. I mean the function declaration from stdio.h you need.

    Read the article

  • Is this a correct syntax (c code found on wikipedia)?

    - by m4design
    I just found this code on wikipedia. Link: http://en.wikipedia.org/wiki/Sizeof#Use The code: /* the following code illustrates the use of sizeof * with variables and expressions (no parentheses needed), * and with type names (parentheses needed) */ char c; printf("%zu,%zu", sizeof c, sizeof(int)); It states that: "The z prefix should be used to print it, because the actual size can differ on each architecture." I tried it on my compiler, but it gives the following result: 'zu,zu'

    Read the article

  • Without width printing format in C

    - by yCalleecharan
    Hi, in C if I have a printf statement containing say "%.2f", it says that the precision is 2 digits after the decimal place. I haven't explicitly specify the width. I have two questions: Is this good programming practice?; Is without specifying the width means that the width of the field will get adjusted automatically when printing the number, irrespective of the number of digits it contains? Thanks a lot...

    Read the article

  • C to C++ Conversion [closed]

    - by Annalyne
    Can someone convert this code to C++, pretty please? :( #include <stdio.h> #include <stdlib.h> #include <time.h> #define WEAPON_ROPE 10 #define WEAPON_REVOLVER 20 #define WEAPON_LEADPIPE 30 #define WEAPON_CANDLESTICK 40 #define WEAPON_KNIFE 50 #define WEAPON_WRENCH 60 #define PEOPLE_MRGREEN 100 #define PEOPLE_MSSCARLET 200 #define PEOPLE_CONLMUSTARD 300 #define PEOPLE_PROFPLUM 400 #define PEOPLE_MISPEACOCK 500 #define PEOPLE_MISWHITE 600 #define PLACE_KITCHEN 1 #define PLACE_HALL 2 #define PLACE_POOLROOM 3 #define PLACE_STUDY 4 #define PLACE_LOUNG 5 #define PLACE_LIBRARY 6 #define PLACE_CONSERVATORY 7 #define PLACE_DINING 8 #define PLACE_BILLIARDS 9 int main() { int die = 0; int players[6][9] = {{0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}}; int allCards[] = {WEAPON_ROPE, WEAPON_REVOLVER, WEAPON_LEADPIPE, WEAPON_CANDLESTICK, WEAPON_CANDLESTICK, WEAPON_KNIFE, WEAPON_WRENCH, PEOPLE_MRGREEN, PEOPLE_MSSCARLET, PEOPLE_CONLMUSTARD, PEOPLE_CONLMUSTARD, PEOPLE_PROFPLUM, PEOPLE_MISPEACOCK, PEOPLE_MISWHITE, PLACE_KITCHEN, PLACE_HALL, PLACE_POOLROOM, PLACE_STUDY, PLACE_LOUNG, PLACE_LIBRARY, PLACE_CONSERVATORY, PLACE_DINING, PLACE_BILLIARDS}; int deckSize = 23; // number of cards in allCards array int count; for (count = 0; count < deckSize; ++count) { printf(", %d", allCards[count]); } // End for // These three array's are so you can put a card back, if need be... int weaponCards[] = {WEAPON_ROPE, WEAPON_REVOLVER, WEAPON_LEADPIPE, WEAPON_CANDLESTICK, WEAPON_CANDLESTICK, WEAPON_KNIFE, WEAPON_WRENCH}; int weaponDeckSize = 7; int peopleCards[] = {PEOPLE_MRGREEN, PEOPLE_MSSCARLET, PEOPLE_CONLMUSTARD, PEOPLE_CONLMUSTARD, PEOPLE_PROFPLUM, PEOPLE_MISPEACOCK, PEOPLE_MISWHITE}; int peopleDeckSize = 7; int placeCards[] = {PLACE_KITCHEN, PLACE_HALL, PLACE_POOLROOM, PLACE_STUDY, PLACE_LOUNG, PLACE_LIBRARY, PLACE_CONSERVATORY, PLACE_DINING, PLACE_BILLIARDS}; int placeDeckSize = 9; srand(clock()); // seed rand() using clock() which gives // the current tick your processor is at... int killer[3]; // no need to initialize yet. killer[0-2] will initialize int deckShuffle = rand() % weaponDeckSize; // picks one number out of the deck killer[0] = weaponCards[deckShuffle]; allCards[deckShuffle] = 0; // Card drawn. No longer exists in deck deckShuffle = rand() % peopleDeckSize; // picks another random card out of the deck killer[1] = peopleCards[deckShuffle]; allCards[deckShuffle + weaponDeckSize] = 0; // Card drawn. No longer exists in deck deckShuffle = rand() % placeDeckSize; // randomly picks the last card needed killer[2] = placeCards[deckShuffle]; allCards[deckShuffle + weaponDeckSize + peopleDeckSize] = 0; // Card drawn. No longer exists in deck int numberOfCards = 0; printf("CLUE\n"); printf("written by John Schintone\n"); printf("Origonal game delvoped by Hasbro\n"); int numberOfPlayers = 0; while ((numberOfPlayers < 3) || (numberOfPlayers > 6)) { printf("How many players are Going to play :\n"); printf("[number] > "); scanf("%d",&numberOfPlayers); // A very fast if statement which only uses integers/char's switch(numberOfPlayers) { case 6: { numberOfCards = 3; } break; case 5: { numberOfCards = 4; } break; case 4: { numberOfCards = 5; } break; case 3: { numberOfCards = 6; } break; default: { printf("You must enter a number between 3 and 6...\n"); } // End default } // End switch } // End while int index1, index2; // Note: ++index1; is faster than index1++; and will almost always // produce better code (index1++ happens after this statement line. // ++index1 increments index1 before this statement line) for (index1 = 0; index1 < numberOfPlayers; ++index1) { printf("Player %d", index1); for (index2 = 0; index2 < numberOfCards; ++index2) { // Remember that allCards[deckShuffle] == 0 because we removed that // card ages ago... works out well, just don't forget you did that : ) while (allCards[deckShuffle] == 0) { deckShuffle = rand() % deckSize; } // End while players[index1][index2] = allCards[deckShuffle]; allCards[deckShuffle] = 0; // Card removed for after loop... printf(", %d", players[index1][index2]); switch(players[index1][index2]) { case WEAPON_ROPE: { } break; // Add more... case PEOPLE_MRGREEN: { } break; // Add more... case PLACE_KITCHEN: { } break; // Add more... default: { printf("Program has caught player %d cheating...", index1); } // End default } // End switch } // End for printf("\n"); } // End for printf("The killer is %d, with the %d, and in the %d \n\n", killer[0], killer[1], killer[2]); printf("Type h for this help... \n"); printf("Type e to escape... \n"); printf("Type r to roll the die... \n"); char command = '\0'; // \0 represents zero, or the null character while (command != 'e') { printf("[one character] > "); scanf("%c", &command); if (command == 'r') { die = rand() % 6 + 1; printf("Your number is: %d \n", die); } // end while if (command == 'h') { printf("Type h for this help... \n"); printf("Type e to escape... \n"); printf("Type r to roll the die... \n"); } // End if printf("\n"); } // End while return(0); // Success. Program worked ok } // End main() Function

    Read the article

  • Pretty output question

    - by sid_com
    Hello! #!/usr/bin/env perl use warnings; use strict; my $text = 'hello ' x 30; printf "%-20s : %s\n", 'very important text', $text; the output of this script looks more ore less like this: very important text : hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello ... but I would like an output like this: very important text: hello hello hello hello hello hello hello hello hello hello hello hello ... How could I change my script to reach my goal?

    Read the article

  • php : echo"", print(), printf()

    - by marc-andre menard
    Is there a better way to output data to html page with PHP ? if i like to make a div with some var in php i will write something like that print ('<div>'.$var.'</div>); or echo "'<div>'.$var.'</div>'"; what is the PROPER way to do that ? or a better way, fill a $tempvar and print it once? like that: $tempvar = '<div>'.$var.'</div>' print ($tempvar); in fact, in real life, the var will be fill with much more !

    Read the article

  • How do you use printf from Assembly?

    - by bobobobo
    I have an MSVC++ project set up to compile and run assembly code. In main.c: #include <stdio.h> void go() ; int main() { go() ; // call the asm routine } In go.asm: .586 .model flat, c .code go PROC invoke puts,"hi" RET go ENDP end But when I compile and run, I get an error in go.asm: error A2006: undefined symbol : puts How do I define the symbols in <stdio.h> for the .asm files in the project?

    Read the article

< Previous Page | 1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >