if-else

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
#include <conio.h>


main()

{
int a,b;
printf("irasykite skaiciu\n");
scanf("%d" , a);
if (a>0)
a=a;
else a=-b;
printf ("%d\n" , a);

return 0;
}

In this program , negative number should become positive number , and positive number remain positive.Help?
Last edited on
This is a c++ board. Your code, while valid c++ in the sense that it compiles, is in fact more c than it is c++. I suggest that you get a book like the "C++ Primer" or "The C++ Programming Language" (beware, it was published before the standard and has some flaws, however, if you want the standard, get the standard, this book is still great). To your problem:
5: it is not main(), it is int main()
9: printf is a c function, use operator<<
10: scanf is a c function, use operator>>, in combination with std::stringstream if needed
12: What a statement. Why not check a<0 and omit the else block?
13: b is not initialized. It's value is undefined.
While not trying to overrule my friend up here, I want to provide a shorter solution:
Replace #include <stdio.h> with #include <cstdio>
Remove #include <conio.h>, and never use it again. Ever.
Replace main() with int main()
Do not declare b. You're not using it.
Replace
1
2
3
if (a>0)
a=a;
else a=-b;

with
1
2
if (a<0)
    a=-a;

You're done.

Oh, and don't use expressions such as a=a. While that doesn't have much of an effect on primitives, objects may not be so forgiving, and using expressions like that might consume ridiculous amounts of time.
ok , thx , sorry for C code.
Topic archived. No new replies allowed.