Post your C/C++ Programs Here

Status
Not open for further replies.

mehulved

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

Yeah BTW, version 0.2 is done now of the same program, with if-else if replaced with switch-case on recommendations of people on IRC. I have also broken down the conversion part into a different file. It is available at *blog.mehulved.com/conversion-0.2.tar.bz2
Some more work going on.
 

QwertyManiac

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

Its better if you make it into functions than switch cases evaluating it, speaking for programs that would "scale" more.
 

mehulved

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

Its better if you make it into functions than switch cases evaluating it, speaking for programs that would "scale" more.
Yeah I have been thinking of something on that lines since the morning but yours is a better idea. I will harass you if I face any problems in this :p
BTW, can someone tell me how to make C program wait for keyboard input. I tried getchar() but doesn't work, I guess it is because it is inside a loop or doesn't have exit after it. I will paste the relevant code in a while.
 

mehulved

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

getchar() is available in stdio.h
yes getch() from ncurses is an option but I would prefer if there's a way without adding additional header files.
 

QwertyManiac

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

Is what I meant, getch() like functionality is not in standard C, probably OS stuff thats why...
 

QwertyManiac

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

That works as its intended to. The characters are in a buffer until return is hit while input, and till then the function does not know that any character has been input or not. getch() or getche() non-standard implementations see each character entered as you do and thus can be used for keyboard shortcuts stuff.
 

aniket.awati

I am the Legend.........
Re: Post ur C/C++ Programs Here

Mehul. How about while(!kbhit).this will keep refreshing the o/p screen and put a grand loop to get the o/p by getch or getchar whatever. put it just after the loop(inner),so that getch or getchar takes input from buffer.
 
Last edited:

c2tarun

N3CrO..NiNj@**
Re: Post ur C/C++ Programs Here

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

int* pntr(int *m)
{
for (int i=0;i<5;i++)
{
scanf("%d",m);
m++;
}
return (m);
}

void main()
{
int a;
int *k;
k=pntr(&a);
for(int i=0;i<5;i++)
{
k--;
printf("%d\t",*k);
}
}

/* This program should take 5 numbers from user and print them in the reverse order as they entered.........
It's a simple program but its not working!!!!!!
It is printing 5 number and then some more worthless characters in turbo c++
Can anyone plz tell me why this is happening */
 

Sykora

I see right through you.
Re: Post ur C/C++ Programs Here

You're trying to store 5 integers when you've only allocated space for 1. Declare a as an array, and see what happens.
 

c2tarun

N3CrO..NiNj@**
Re: Post ur C/C++ Programs Here

thank you for suggesting to use an array sykora.........
i know that will work..........
what the problem is i am storing the number at different memory locations and retreiving them from there only.........
i am also changing the memory location........
and after all it is printing the output but with useless characters..........
/* IF POSSIBLE DON'T TELL ME ANY ALTERNATIVES, TELL ME WHY THIS IS WRONG */
THANK YOU
 

aniket.awati

I am the Legend.........
Re: Post ur C/C++ Programs Here

what the problem is i am storing the number at different memory locations and retreiving them from there only.........
i am also changing the memory location........

I think what sykora means by an array is array of pointers. If you use that you will get different memory locations AND they will be static so that you will retrieve them from there only.

And, the some garbage o/p that you get after the printing is due to the null characters in the
buffer. scanf leaves null character on the buffer, so you would better use flushall() if you use scanf in a loop.

Again you will get normal o/p if you just replace printf/scanf by cout/cin.
 
Last edited:

ngtw

Right off the assembly line
Re: Post ur C/C++ Programs Here

What is the c++ code to find the prime number for let the user to input the integer? Here is my java code help me translate to c++ code.
int i, a, sum=1;

for(i=2; i<=50;i++) {
for(a=2; a<i; a++) {

if ((i%a)==0)
sum=0;
}

if (sum==1)

System.out.println(i+" ");

sum=1;
}

Help me change my java code to c++ code.
for (int i=0; i<10; i++)
System.out.print(fib(i)+", ");


public static int fib(int n) {
if (n < 2)
return n;

else

return fib(n-1)+fib(n-2);
}
 
Last edited:

Sykora

I see right through you.
Re: Post ur C/C++ Programs Here

[You're trying to store 5 integers when you've only allocated space for 1. Declare a as an array, and see what happens.

/* IF POSSIBLE DON'T TELL ME ANY ALTERNATIVES, TELL ME WHY THIS IS WRONG */

Did you even read my entire post? It wasn't that long.

You're allocating space for _one_ integer here :


You're then creating a pointer to it here :


But then, you're treating that pointer like an array, here :

int* pntr(int *m)
{
for (int i=0;i<5;i++)
{
scanf("%d",m);
m++;
}
return (m);
}

In doing this, you are accessing memory outside of the space that you declared with "int a;". This memory outside your variable is _not_ guaranteed to remain the same between accesses.

As you are using a screwed compiler in Turbo C, it ignores this and prints whatever is there at the time, which is junk. When I compile your program with gcc, it segfaults, and with good reason.
 

achalaxp

Right off the assembly line
Re: Post ur C/C++ Programs Here

Please reply if U know the meaning of "1LL<< x" in C language

Thanx
 

mehulved

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

Please reply if U know the meaning of "1LL<< x" in C language

Thanx
Did you ever try anything? How much do you understand?

Try this program, it may help you understand *rafb.net/p/5tSNGv95.html
 
Last edited:
Status
Not open for further replies.
Top Bottom