FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


What's wrong with this C code???





falconfx
Hello. I have a problem with this tiny C program.

Code:

#include <stdio.h>

int main()
{
   int n;

   printf("Enter a number: ");
   scanf("%d", n);

   printf("The result is %d.\n", n * n);

   return 0;
}


It should print the square value of n, but it prints gargabe in the second 'printf'. Any suggestion?
masterevil
u wrote %d in ""s. make it
printf("The result is\n"%d , n * n);
something like this but i cannot remember c. i use c++
it will be like this in c++:


#include <iostream.h>
#include <conio.h>

void main()
{
int n;
cout << "enter the number: ";
cin >> n;
cout << endl << "you have just entered " << n;
getch();
}
Yeah, simple and working...
kv
change

scanf("%d", n);

to

scanf("%d", &n);

It should work.
aloSegun2
hahahaha, you should try c++, it's much easier to use
example
Code:

int n;
printf("ENTER A NUMBER");
scanf("%d",&n);
printf("THE NUMBER IS: %d",n*n);

change it to
Code:

int n;
cout<<"ENTER A NUMBER";
cin>>n;
cout<<"THE NUMBER IS:"<<n*n;
Donutey
^^
keep the return 0; bit you had in your c code... C++ is a lot easier for doing this stuff.
Code:


int main();
{
double number;
cout << "Enter the number you want squared: ";
cin >> number;
cout << number * number << endl;
return 0;
}


using double would be a good idea here if you want to get an answer above 32,728 (those last three right? idk i forget lol)
SystemWisdom
Sticking strictly to C from your original post:
Code:

#include <stdio.h>

int main()
{
   int n;

   printf("Enter a number: ");
   scanf("%d", &n);   // <---  &n = the Address Of 'n'

   printf("The result is %d.\n", n * n);

   return 0;
}


And Donutey, you were thinking of the max size of a signed integer?

Unsigned int = 65536 - 1
Signed int = (65536 / 2) - 1 = 32768 - 1

But you were close!! Only one digit was wrong!! Smile
Donutey
knew it was somewhere round there Smile
rohan2kool
hmm... can anyone tell me, what is 'cout'. Even i use C, and want to switch to C++...
pavelt
rohan2kool wrote:
hmm... can anyone tell me, what is 'cout'. Even i use C, and want to switch to C++...


cout and cin are global streams for output and input a data for console. The streams support overloaded operators >> and << for output and input.

You could simply write
cout << SomeYouVar << "some test" << "\n";

see header iostream for details.
mars
kv wrote:
change

scanf("%d", n);

to

scanf("%d", &n);

It should work.



yeah,you should use n 's address.
Related topics
10 frih$ contest: what does this c++ code do?
Slight problem with C code
C++ TUTORIAL : INTRODUCTION TO C++
C++ code help
This C code is little unclear to me.
I'm doing something wrong,,,
C++ code problem
C++
C++ code
Explaination about this C++ code needed
What is wrong with this MySQL query?
C++ Codes Tips Tricks for competition...
Funny with C code.
Handling /dev/dsp in C
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.