C/C++ Beginner's Guide and Post Basic Questions here

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.
 

krishnandu.sarkar

Simply a DIGITian
Staff member
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);
}
pls check for run time error..
its a basic 2 player tic tac toe program

#1 Function should be declared above
#2 Function definition should be outside main()
#3 Call the function inside main()
 

icebags

Technomancer
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?
 

rijinpk1

Aspiring Novelist
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?

can you elaborate a little more. i think what you want is to concatenate two strings by using strcat function.
 
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?

with arrays:

Code:
    char str1[] = "Hello ";
    char str2[] = "World!";
    char str3[30];
    strcat (str, str1);
    strcat (str, str2);

    cout << str;

with pointers:

Code:
    char *str1 = "hello ";
    char *str2 = "world!";
    char *str3;

    str3 = (char*)malloc( strlen(str1) + strlen(str2) + 1 );
    
    strcpy(str3, str1);
    strcat(str3, str2);

    cout << str3;

with 'string':

Code:
    string str1 = "hello ";
    string str2 = "world!";
    string str3;

    str3 = str1 + str2;
    cout << str3;

You can also implement a concat function manually or overload the '+' operator manually;
 

icebags

Technomancer
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. :D

can you elaborate a little more. i think what you want is to concatenate two strings by using strcat function.

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.
 
Last edited:
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. :D



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.

If you are working with C++, you can overload the plus operator to concatenate strings.
 

icebags

Technomancer
arduino accepting arrays and pointers methods, but it doesn't recognize string as datatype.

any suggestion on concatenating numbers and numeric variables with strings ?
 

rijinpk1

Aspiring Novelist
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.
 

icebags

Technomancer
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.

the ide is build with java alright. i am gonna try what u said, but is this the normal way? i think there was something like typecasting ? *l.yimg.com/us.yimg.com/i/mesg/emoticons7/39.gif

Which language are you using, C or C++?

u can take it i am basically using C. but the ide supports some C++ stuff as it says here :
Frequently Asked Questions

if u want to try u can get it here.
 

ashs1

Padawan
hi guys..need a small help..
we have C language in our college course
Unfortunately, i was absent when they taught us pointers, structure, union,..
Though i got through the exam somehow, now we have practicals this week( viva too) & we also have c++ in next sem.
Can you guys suggest any sites which can help me easily understand pointers, structure, union ??
I am just a beginner on C, so if possible, please suggest those sites which can help me understand these concepts easily ( i am fairly thorough in looping now. :) )

Thanks. :)
 

flyingcow

Shibe
I am building a calculator in c++ i am not building it via using functions
i want to display "invalid selection" if the user types in anything other than 1,2,3,4 as his selection
i tried
if(selection==1)
{xyz}
else
cout<<"Invalid chooice";
if (selection== 2)
{xyzz}
else
cout<<"invalid seklection";
but it isnt working... i also tried
if()
{xyz}
if()
{xyz}
then putting in cout
and also tried
if{
if()
{}
if()
{}
}
else
cout
help..
#include <iostream>
using namespace std;
//my first program(C++) CALCULATOR v1.0.0
int main()
{
float x, y, z, sum, prod, div, sub;
cout<<"Prototype Calculator v 1.0.0\n\n";
cout<<"Select the type of calculation you want to perform:\n";
cout<<"1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n";
int selection;
cin>>selection;

if (selection==1){
cout<<"Enter the first number\n";
cin>>x;
cout<<"Enter the second number\n";
cin>>y;
sum=x+y;
cout<<"Sum= "<<sum;
}

if(selection==4){
cout<<"Enter the first number\n";
cin>>x;
cout<<"Enter the second number\n";
cin>>y;
div=x/y;
cout<<"Division= "<<div;
}

if(selection==3){
cout<<"Enter the first number\n";
cin>>x;
cout<<"Enter the second number\n";
cin>>y;
prod=x*y;
cout<<"Product= "<<prod;}

if(selection==2){
cout<<"Enter the first number\n";
cin>>x;
cout<<"Enter the second number\n";
cin>>y;
sub=x-y;
cout<<"Subtraction= "<<sub;}


}
 

Vyom

The Power of x480
Staff member
Admin
^^ 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".

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

*www.codeblocks.org/downloads
 

flyingcow

Shibe
^^ 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".
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
 
Last edited:

rohitshubham

Ambassador of Buzz
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
 

flyingcow

Shibe
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 :)
 
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 :)

There are two methods for getting pseudo random numbers withing a range not starting from 1:

method 1:

MIN + (rand() % (MAX - MIN + 1 ))

method 2:

Code:
#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
}

method 2 is the modern, preferred approach.

Source: Random number c++ in some range - Stack Overflow
 
Top Bottom