What is causing this weird output? [closed] - c

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have the following code:
void doPlayerMove(void)
{
bool moved = false;
while (!moved)
{
printf("\nWhere is the piece you want to move?(rc):");
int r = getchar() - '0';// gets the row number
int c = getchar() - '0';// gets the column number
printf("%d:%d", r, c);// prints the chosen row/column
clearInput();
printf("\nWhere is the space you want to move to?(rc):");
int r2 = getchar() - '0';
int c2 = getchar() - '0';
printf("%d:%d", r2, c2);
...
}
}
void clearInput(void)
{
while(getchar() != '\n');
}
this is the output i get:
Where is the piece you want to move?(rc):51
5:1
Where is the space you want to move to?(rc):40
4:00
Whats up with the extra 0? Does anyone see where the problem is?

As the OP says in a comment:
Problem solved, it was some output from some function i was calling in
the ... sorry for the false alarm!

Related

how can I covert int to char in C [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I'm trying to covert an int to char. Is there any way to do that?
For example:
{
int i;
char d;
i = 55;
d = i;
printf("%c\n", d);
}
How do I make d = 55?
If you want to put the number 55 into a string, use sprintf
Indeed your example can do what you want.
If you really want to place safe, you may:
d = (char) i;
Try this code segment:
printf("%d\n", d);
char are presented in the memory as binary format wich is equivalent to a number and this number is called a code ascii. when you print the code ascii with "%c" Then it will print the charchter equivalent to this code ascii

What is wrong with my code [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I have to write a program in c language and this is the code : The problem is that when I try to compile it it says : syntax error before return .Where is my error?
#include <stdio.h>
int main (void)
{
char i,c2,j;
int c=4;
i=j=3;
while (++i <=c)
{
int j=1;
printf("\n Nr1=%c Nr2=%d",64+i,c2);
} do;
return 1;
}
Remove the do from your code. Just while (++i <= c) { /* ... */ };
You are redeclaring j inside the while loop. Remove int j=1;
and remove the do; at the end of your while
There is no such thing as while ... do loop in C. There are while loops or do ... while loops.

C printing float arrays into columns [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I'm trying to print out an array of 100 lf values into columns of ten. I was planning on inserting a new line whenever I hit the tenth data value in each line, but I'm having trouble with my if statement. "Left operand must be 1-value" is the error I keep running into.
Here's the code as is:
for (x=0; x<100; x++)
{
if (x % 10 = 0)
{
printf("\n");
}
printf("|%-6.2lf|", i[x]);
}
Is there a cleaner way to do this?
The problem is this line:
if (x % 10 = 0)
That should be the double-equals sign:
if (x % 10 == 0)
A common typo.

changing a while loop into a for loop in ANSI c80 [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am trying to change this into a for loop but im not sure how to.
while((c = getchar()) != EOF){
tweet[cnt] = c;
++cnt;
}
for(cnt = 0; (c = getchar()) != EOF; cnt++)
{
tweet[cnt] = c;
}
While loops and for loops are not so different. The while loop contains only an comparison, if its true it is executed once more until the comparison is made and it not true any more.
The for statement is the same only contains 3 parts separated by ';'.
the first is the part you want to do one time before entering the ever ending cycle.
The second part is the same as the while loop.
The third part is what you want to do after one cycle is done before getting to the next comparison for entering again.
Lot of compilers accept more than one statement in the first and last part. In this case you could get this:
for(cnt=0; , x=0 ; (c=getchar()) != EOF; CNT++, x++){
tweet[cnt] = c;
}
int TheNumberOfCyclesMade = x;

don't know why the program is crashing [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have written a code to find nth ugly number(a number which has at least one prime factor grater than 5) where n is a given input. my program runs well if the user inputs something less than 240. But if input gets bigger than that, program crashes!. My question is if it is a time consuming problem then it should take time but why the program crashes? I have used double everywhere so it might not be the matter of variable capacity!!
my code is below:
#include<stdio.h>
#include<math.h>
double primes[1000]={2,3,5};
int serial=3;
double next_prime()
{
double f=primes[serial-1]+2;
int count;
for(count=1;primes[count]<=(sqrt(f)+1) && count<serial;count++){
if(fmod(f,primes[count])==0){
f+=2;
count=1;
}
}
return primes[serial++]=f;
}
int main()
{
double ugly_serial=12,ugly_number=16,j;
int c,count,loop,input;
scanf("%d",&input);
while(ugly_serial<input)
{
loop=0;
for(c=3;primes[c-1]<=sqrt(ugly_number);c++){
j=next_prime();
}
for(count=3;count<c;count++){
if(fmod(ugly_number,primes[count])==0){
loop=1;
break;
}
}
if(loop==0){ugly_serial++;}
ugly_number++;
}
printf("%.0lf",ugly_number);
return 0;
}
I have compiled and run your code. The program works fine with all the input I have tried, including 56565.
Are you sure you are running the most recently compiled version of your program?

Resources