Author Topic: Problem with C  (Read 1076 times)

I have a problem with coding with C
I've got a small assignment to create a program to relay a number input to a output displaying the number on separate lines with asterics to represent numbers.
For example, if I input the number 1234 it will show as
4 ****
3 ***
2 **
1 *

Not sure why I'm having a problem with this. Please give me a hand :)
My apologies if this is in the wrong section.

Code: [Select]
#include <stdio.h>
#include <stdlib.h>

int main() {

int n, star;

printf("Enter n");
scan("%d", &n);

while (n > 0)
{
printf("\n%d", n % 10);
star = n % 10;
while (star > 0)
{
printf("*");
star--;
}
printf("n%d\n");
n /= 10;
}

return 0;
}

This is the torquescript coding board, not the C coding board. As far as I know this kind of thing belongs in off-topic.

Also, what about this doesn't work? I'm no C expert but that looks just fine.

This is the torquescript coding board, not the C coding board. As far as I know this kind of thing belongs in off-topic.

Also, what about this doesn't work? I'm no C expert but that looks just fine.
ah my bad, I didn't read the board description.
And that's why I'm here to ask, not sure why it keeps coming up with an error.

Please fix the indenting
The inner while loop and the two lines after it should be indented. At first glance it looked like you were missing a bracket


Why are you moduloing and dividing by ten?
It seems to me like youd just want n and n--

Oh never mind I thought you were using 4 for input and counting down



What is this "error" is giving? More details please. You can't just say "it doesn't work"
« Last Edit: February 25, 2015, 12:29:23 PM by Headcrab Zombie »

Why are you moduloing and dividing by ten?
To get each individual digit, if you take a number % 10 you get the last digit

To get each individual digit, if you take a number % 10 you get the last digit
Yeah I misread what he was using for input. I've edited it
« Last Edit: February 25, 2015, 12:32:21 PM by Headcrab Zombie »

What is this "error" is giving? More details please. You can't just say "it doesn't work"

after compiling, I get this...


Might go to my lecturer with this problem, but I would rather not.

Yeah I have no idea what that means

I think the problem is your division by 10 (n/=10;) is yielding a float (432.1), which you're trying to implicitly convert to int, which only accepts whole numbers. Look up how to truncate the decimal and cast to int, I'm not sure how to do it in c ( n = (int)(n/10); is off the top of my head and worth trying)
That's the only thing I can think of
« Last Edit: February 25, 2015, 12:42:38 PM by Headcrab Zombie »

What kind of error is it? Syntax, stack, overflow, illegal arguments? What compiler are you using? I personally recommend Codeblocks and Turbo C. What file is that? That doesn't look like your code. That looks like low-level header code.

Linker error. You're using a function that doesn't exist.



I compiled the code for Real-Time, medium model, speed priority, using Turbo C. It's not scan(). It's scanf(). I have the compiled program if you want me to pass it. (Since I compiled for Real-Time, it will NOT work on x64 systems without a DOS emulator.)

Other than that, it works as you stated it should.

Code: [Select]
#include <stdio.h>
#include <stdlib.h>

void main() {

int n, star;

printf("Enter n");
scanf("%d", &n);

while (n > 0)
{
printf("\n%d", n % 10);
star = n % 10;
while (star > 0)
{
printf("*");
star--;
}
printf("n%d\n");
n /= 10;
}
}

Except that the output is the following:
Code: [Select]
Enter n1234
4****n1224
3***n1224
2**n1224
1*n1224



Quick edit, no matter what I input, the print is always *n1224.



Also, numbers with more than 4 digits causes the program to quit.

l8 edit: Also, avoid doing that C++ bullstuff. If your function doesn't need to return anything, use void or nothing at all. Not int functionname and then return 0;
« Last Edit: February 26, 2015, 08:45:28 AM by Pie Crust »

l8 edit: Also, avoid doing that C++ bullstuff. If your function doesn't need to return anything, use void or nothing at all. Not int functionname and then return 0;
this is not bullstuff, it's something that i'm at least 70% certain is in the C standards

you declare that main returns an int, then return an int
this is passed back to the operating system to determine if the program succeeded or not

further, you do not declare void main(void) because main does return stuff.

this is not bullstuff, it's something that i'm at least 70% certain is in the C standards

you declare that main returns an int, then return an int
this is passed back to the operating system to determine if the program succeeded or not

further, you do not declare void main(void) because main does return stuff.
Correct

If your function doesn't need to return anything, use void or nothing at all.

void means that your function returns "nothing at all". Specifying no return type at all is exactly the wrong way to go about it. Some compilers will let you do it in exceptional cases and they'll default it to returning an int (and others will just say your code is invalid). Always specify the return type when declaring a function.

I've never had any problems using void main() or main() directly with the compilers I use or with running my software. The more you know...

I've never had any problems using void main() or main() directly with the compilers I use or with running my software.
Everyone will wish you were dead if you do void main()
I learned the programming community has a very strong opinion on this some time ago