Post your C/C++ Programs Here

Status
Not open for further replies.

QwertyManiac

Commander in Chief
Re: Post ur C/C++ Programs Here

This ain't a passing area :p

Anyway global declaration of a variable or function makes it available to be used throughout the program.

See the variable A in the following example:

Code:
#include<stdio.h>

int A=10;

void DispA()
      {
            printf("\nGlobal A used in DispA() = %d",A);

                  {
                        int A = 30;
                        printf("\nLocal A for DispA() = %d",A);
                  }
      }

int main()
      {
            printf("Global A = %d\n",A);
                  {
                        int A = 20;
                        printf("Local A for Main() = %d\n",A);
                  }
            DispA();
            return 0;
      }
Running the above would give you a clear example of what global declaration means.
 

ratedrsuperstar

The Sexy Beast
Re: Post ur C/C++ Programs Here

hey can anyone post a program to display this in c++

A
AB
ABC
ABCD
ABCDE
sloping on both sides


& PASCAL'S triangle using for loops and without <iomanip.h> (if possible)
 

ilugd

Beware of the innocent
Re: Post ur C/C++ Programs Here

find the length of the string and do a for loop from 1 to length and another from 1 to i in the first loop printing each character. When the inner loop completes, print a newline. Should work.

Just installed centos on friday and trying to get gcc to work. other wise would have posted the entire program. :-D

ok, got it working
checked and works
Code:
#include <iostream>
using namespace std;

int main()
{
  char str[255];
  cout<<"Enter the String ";
  cin>> str;
  for (int i=0; i<strlen(str); i++)
    {
      for (int j=0; j<=i; j++)
          cout<<str[j];
      cout<<endl;
      }

  cout<<str;
  return 0;
  }
 
Last edited:

Quiz_Master

* Teh Flirt King *
Re: Post ur C/C++ Programs Here

Here is my todays Homework : (Just complted this late in night...uhmm... sorry morning...;) )
Its a fairly simple program...

This is a program which compare two strings, concatenate them and then gives the length of string.

Code:
/* Program to compare, concatenate and get length of string by Ashwin */

#include<iostream.h>
#include<conio.h>
#include<string.h>
main()
{
   clrscr();
   [COLOR="Blue"]char[/COLOR] a[50],b[25];
   cout<<"Enter First String:=  ";
   cin>>a;
   cout<<"Enter Second String:=  ";
   cin>>b;
   [COLOR="#0000ff"]if[/COLOR] (strcmp(a, b) ==0)
   cout<<"Both Strings are same.\n";
   [COLOR="#0000ff"]else[/COLOR]
   cout<<"Both Strings are not same\n";
   strcat(a,b);
   cout<<"The Combined String Is:= "<<a;
   cout<<"\nThe length of string is:= "<<strlen(a);

   getch();
   [COLOR="#0000ff"]return[/COLOR] 0;
}

Thats it for now.... I know its very basic .... But maybe someone on this forum can use this for school homework.:p
(And I am ashamed to say that they teach us all this in college level:mad: )
 

The_Devil_Himself

die blizzard die! D3?
Re: Post ur C/C++ Programs Here

@ilugd dude whats that "using namespace std;" for.I have seen it a lot but don't know exacdtly what it does.
 

ilugd

Beware of the innocent
Re: Post ur C/C++ Programs Here

do a google search for namespaces. It is like package in a few other languages. Most of c++ standard functions and classes belong to this. If i hadn't used that, i would have had to prefix each cin and cout call with std like std::cin and std::cout
 

aditya.shevade

Console Junkie
Re: Post ur C/C++ Programs Here

In short :- It tells the compiler which function/var/stream to use.

If you define a function x and there is a function named x present in a library you are using, then compiler will not understand which x to call (if the defs are same). So you can tell the compiler using namespaces, about the function you wanna use.
 

ilugd

Beware of the innocent
Re: Post ur C/C++ Programs Here

right. This explanation is definitely better. Now i know why parents of my friends pay me NOT to teach them.
 

nithinks

True Techie
Program to kill a software /demo version expire in C

Hi guys....

These code snippets written by me.. can make a program written C to expire after certain number of executions. Check this out.

This program is to create a software that expires after 15 executions (I like coding such programs) the technique I used is first program here creates a Encrypted counter file named VALIDITY.DAT in your C drive that contains the counter but nobody can read or modify that.
I used 10 bit key for encryption here u can specify required number of executions and create the file

Code:
#include< stdio.h>
#include<conio.h>

void main()
{
int num_executions=15,decrypted=0,encrypted=0,i=0,digit=0;
int xor_bit[10];
int bin_num_executions[10];
int key[10]={'1','1','0','1','0','1','1','1','0','1'}; 
FILE *fp;
clrscr();
for(i=0;i<10;i++) //initialize array
{
bin_num_executions[i]=0;
xor_bit[i]=0;
}
//////////////////////////////////////////////////////
i=9;
do //converting to binary, the number of executions 
{
digit=num_executions%2;
bin_num_executions[i]=digit;
i--;
num_executions=num_executions/2;
}while(num_executions!=0);
for(i=0;i<10;i++)
{
printf("%d",bin_num_executions[i]); 
}
//////////////////////////////////////////////////////
//bitwise xoring,writing to file begins here

printf("\n");
for(i=0;i<10;i++)
{
bin_num_executions[i]=bin_num_executions[i]^key[i]; 
if(bin_num_executions[i]==48)
{
xor_bit[i]=0;
}
else
{
xor_bit[i]=1;
}
}//for loop ends here

fp=fopen("C:\\validity.dat","w"); //writing to a file
for(i=0;i<10;i++) 
{
printf("%d",xor_bit[i]);
//fprintf(fp,"%d",xor_bit[i]);
}
fwrite(xor_bit,sizeof(int),10,fp);
fclose(fp);

// printf("\nInitial key %d\n",num_executions);
// printf("Encrypted %d",encrypted); 
getch();
}
this code is for our software which reads the encrypted file, decrypts it and decreases the counter then updates file encrypted
Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int validate()
{
int init_counter[10],cnt,n; 
int i,k=9;
int product=1;
int bin_num_executions[10],xor_bit[10],digit;
int totl=0;
int total_copy=0;
int key[10]={'1','1','0','1','0','1','1','1','0','1'}; 
FILE *fp;
for(i=0;i<10;i++) //initializer
{
init_counter[i]=0;
bin_num_executions[i]=0;
xor_bit[i]=0;
}

fp=fopen("C:\\validity.dat","r");
fread(init_counter,sizeof(int),10,fp); 
fclose(fp);
//decoding
for(i=0;i<10;i++)
{
init_counter[i]=init_counter[i]^key[i];
if(init_counter[i]==48)
{
init_counter[i]=0;
}
else
{
init_counter[i]=1;
}
}
//converting binary init_counter to decimal
for(i=0;i<10;i++)
{
totl=totl+(init_counter[i]*pow(2,k));
k--;
}
totl=totl-1; //reduce counter
total_copy=totl;
/////////////////////////////////////////////////////////////// 
//updating to the file

i=9;
do //converting to binary, the number of executions
{
digit=totl%2;
bin_num_executions[i]=digit;
i--;
totl=totl/2;
}while(totl!=0);
////////////////////////////////////////////////////// 
//bitwise xoring,writing to file begins here


for(i=0;i<10;i++)
{
bin_num_executions[i]=bin_num_executions[i]^key[i];
if(bin_num_executions[i]==48)
{
xor_bit[i]=0;
}
else
{
xor_bit[i]=1;
}
}//for loop ends here

fp=fopen("C:\\validity.dat","w"); //writing to a file
fwrite(xor_bit,sizeof(int),10,fp);
fclose(fp);
return(total_copy);
}
////////////////////////////////////////////////////////////////////
void main()
{
int validity=validate();
clrscr();
if(validity==0)
{
printf("\nSoftware Expired\n");
getch();
exit(); 
}
if(!(validity>0&&validity<16))
{
printf("\nCurrupted associated file\n");
getch();
exit();
}
printf("\nYou have %d more executions\n",validity);
getch(); 
} // end of main
If u try to modify any file it gives error.....
 
Last edited:

nithinks

True Techie
Re: Post ur C/C++ Programs Here

mehulved said:
What's the use of wasting time over coding such cripplewares?

Wasting time? I have written a number of codes.. but thought this is a bit different.So I posted.
 

mehulved

18 Till I Die............
Re: Post ur C/C++ Programs Here

nithinks said:
Wasting time? I have written a number of codes.. but thought this is a bit different.So I posted.
That wasn't my point. Anyways let it be.
 

sauravgr8

Broken In
Re: Post ur C/C++ Programs Here

I am unable to run even a simplest graphics program in C, even though when i compile the program it shows success but when i run it , it gives the error that the
BGI error: graphics not initialized...
I dont know where is the problem...can any one help plz
#include<stdio.h>
#include<conio.h>
#include<graphics.h.
void main()
{
int gd=DETECT,GM;
initgraph(&gd,&gm,"");
setbkcolor(RED);
getch();
}
 

ilugd

Beware of the innocent
Re: Post ur C/C++ Programs Here

are you saving the file as a cpp and then compiling as a c++ program? Should work. :-? Works here. I just copy pasted after running gcc and executiing.
 

Projjwal

free world from money
Re: Post ur C/C++ Programs Here

@nithinks Really cool one.
But,pls explain the program.Which Encryption technique u use?
Another thing if user copy the .dat file when it execute first(means .dat file with 15 execution validation) then after expiring de software just overwrite the file then what happened? Software with 15 execution trial or gives an error.
According to ur logic it's worked perfectly for de next 15 execution.
 

nithinks

True Techie
Re: Post ur C/C++ Programs Here

Projjwal said:
@nithinks Really cool one.
But,pls explain the program.Which Encryption technique u use?
Another thing if user copy the .dat file when it execute first(means .dat file with 15 execution validation) then after expiring de software just overwrite the file then what happened? Software with 15 execution trial or gives an error.
According to ur logic it's worked perfectly for de next 15 execution.

thanks.

Yes.. you are right. what we can do is, create that .DAT file in a particular location like C:\all users\application data\, any location and the path of that file we need not disclose.

And coming to encryption , its simple xor operation, nothing more than that

for example let the key be 1010(hidden from user) and counter 3 (0011)
xor this counter and store it in file
that is
0011 ->counter
1010 ->key
--------
1001 (encrypted counter)
--------
to get the value back, xor this encypted counter with the key
1001 ->encrypted counter
1010 ->key
---------
0011 which is 3
---------
 
Last edited:

Projjwal

free world from money
Re: Post ur C/C++ Programs Here

Yea...i got it.but i think u have to use some complex hash algorithm like MD5. & create an startup program(hidden) which will use the dat file.As a result when the user overwrite/delete the file windows automatically blocked it.
 

ilugd

Beware of the innocent
Re: Post ur C/C++ Programs Here

how bout setting some encrypted string on a harddisk sector/track combination and setting the sector to damaged in the allocation table. The program can then read and update. I guess you will have to use interrupts to do that.
 
Status
Not open for further replies.
Top Bottom