rohitshubham
Ambassador of Buzz
^^what compiler are you using coz it's showing error on my GCC compiler as i am pretty sure it's due to the fact that function definitions are not allowed inside the main() function.
pls check for run time error..Code:#include <stdio.h> int main() { char C [3][3]; int C1[3][3]; int i, j; int m[8];int n=0; int k=0; int trigger=0; void check() { for(i=0;i<9;i++) { m[i]=0; } for(i=0;i<3;i++)//ACROSS ADD { for(j=0;j<3;j++) { m[i]=m[i]+C1[i][j]; } } for(i=0;i<3;i++)//DOWN ADD { for(j=0;j<3;j++) { m[i+3]=m[i+3]+C1[j][i]; } } //DIAGONALS ADD m[6]=C1[0][0]+C1[1][1]+C1[2][2]; m[7]=C1[0][2]+C1[1][1]+C1[2][0]; for(i=0;i<8;i++) { if(m[i]==3) {trigger=1; } if(m[i]==6) {trigger=2; } } } void display() { printf("\nState of Game:\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("%c ",C[i][j]); } printf("\n"); } } printf("Welcome to my Tic Tac Toe game\n"); while(k<9) { if(n%2==0) { printf("\nPlayer 1 Input : X at desired index location"); printf("\ni:"); scanf("%d",&i); printf("\nj:"); scanf("%d",&j); C[i][j]='X'; C1[i][j]=1; //CHECK display(); check(); n++; if(trigger==1) { printf("Player 1 wins"); break; } } if(n%2==1) { printf("\nPlayer 2 Input : X at desired index location"); printf("\ni:"); scanf("%d",&i); printf("\nj:"); scanf("%d",&j); C[i][j]='O'; C1[i][j]=2; //CHECK display(); check(); n++; if(trigger==2) { printf("Player 2 wins"); break; } } k++; } return(0); }
its a basic 2 player tic tac toe program
I have not been in touch woth c / c++ for a long time, but need a solution for this. Googling kinda confusing me....
Need to add a few stings/ character variables into a new string variable. How can this be done?
I have not been in touch woth c / c++ for a long time, but need a solution for this. Googling kinda confusing me....
Need to add a few stings/ character variables into a new string variable. How can this be done?
char str1[] = "Hello ";
char str2[] = "World!";
char str3[30];
strcat (str, str1);
strcat (str, str2);
cout << str;
char *str1 = "hello ";
char *str2 = "world!";
char *str3;
str3 = (char*)malloc( strlen(str1) + strlen(str2) + 1 );
strcpy(str3, str1);
strcat(str3, str2);
cout << str3;
string str1 = "hello ";
string str2 = "world!";
string str3;
str3 = str1 + str2;
cout << str3;
can you elaborate a little more. i think what you want is to concatenate two strings by using strcat function.
thanks for the quick reply, i just forgot to mention that i have to concat a number (int/float) with it as well, it would be better if the number can be formatted in something like 00.00, i.e. if its 5.6 then it should show like 05.60 .
thanks again, please pardon my mistake.
well i was trying to concat with +, but i think it didn't work, i m gonna try with harsil said methods one by one.
i am trying to program in arduino environment, where there could be some restrictions and everything may not work like a standard c/c++ program. i need to see.
arduino accepting arrays and pointers methods, but it doesn't recognize string as datatype.
any suggestion on concatenating numbers and numeric variables with strings ?
arduino accepting arrays and pointers methods, but it doesn't recognize string as datatype.
any suggestion on concatenating numbers and numeric variables with strings ?
arduino program is simply java, right? you can use any java ide ,i guess. else you may have to declare an integer array and convert characters to corresponding ASCII values and store it on the integer array and display it using a single quote which should print characters.
Which language are you using, C or C++?
guys i need c and c++ compilers.. my email id is dpnkrborah@gmail.com...it would of great help if u send me the files or provide me with links to download the compilers from
so i have to write^^ You haven't written the code for the selection *not* being equal to 1,2,3 or 4.
You can use any method to make a condition that if selection is not from 1 to 4 then display "Invalid selection".
use the rand() function present in <cstdlib>so i have to write
if(selection !==1&& !==2 && !==3 && !==4)
{
cout<<"Invalid selection";
}
??
EDIT: Thanks vyom, i got it working
also i have another question, i am making a rock paper scissors game and the code currently is
#include <iostream>
using namespace std;
int main()
{
int choice;
cout << "*-Welcome to the unfair Rock-Paper-Scissors game-*\nYou will always lose!!\n" << endl;
cout<<"Select your move:\n\n1.Rock 2.Paper 3.Scissor\n";
cin>>choice;
if (choice==1)
{cout<<"The computer has selected PAPER\n YOU LOSE";}
if (choice==2)
{cout<<"The computer has selected SCISSORS\n YOU LOSE";}
if (choice==3)
{cout<<"The computer has selected ROCK\n YOU LOSE";}
if (choice!=1 && choice!=2 && choice!=3)
cout<<"Invalid Choice";
return 0;
}
i want to make it so that the computer plays random moves, i.e it selects at random, how can i do thqat>?? i just want a code to generate random code, then i will use array to put "a[random digit]" as the computer chice
use the rand() function present in <cstdlib>
int m = rand() % 3; will produce random value between 0 and 2
imt m = rand() % 3+1; will produce random values between 1 and 3
thanks,i searched and found a rand() function but it gave a large value so i did %4 and it always gave me 3...
i will try doing the 3+1 thing, thank you
#include <iostream>
#include <random>
int main()
{
std::random_device rd; // obtain a random number from hardware
std::mt19937 eng(rd()); // seed the generator
std::uniform_int_distribution<> distr(25, 63); // define the range
for(int n=0; n<40; ++n)
std::cout << distr(eng) << ' '; // generate numbers
}