why am i getting a different number after declaring a variable? - c

i am new to C programming and i was trying to write a simple program that asks the user to re-arrange the numbers displayed on the screen but i encountered a problem, i would get a different number printed out on the screen instead of the value i assigned to the variable.why am getting a different number? Here is a screenshot of the problem i am having and my codethe image:
#include <stdio.h>
main()
{
int numOne, numTwo, numThree, ansOne, ansTwo, ansThree;
char name[20];
numOne=34521;
printf("\nWelcome to scrambled numbers Game");
printf("\n Please input your name to get started: ");
scanf("%s", name);
printf("\nRe-arrange this numbers in ascending order %d :", &numOne);
scanf("%d", &ansOne);
if(ansOne==12345)
{
printf("Congratulations %s you have won the first round", name);
}
else
{
printf("sorry %s you failed the first round", name);
}
}

This is happening because you are printing the adress of the variable numOne instead of numOne it self.
Try removing & operator from numOne in your printf.
Replace this:
printf("\nRe-arrange this numbers in ascending order %d :", &numOne);
By this:
printf("\nRe-arrange this numbers in ascending order %d :", numOne);

printf and scanf take different kinds of arguments: printf needs values to be displayed, and scanf needs pointer addresses to receive input data.
The &numOne argument to printf causes the program to show the memory address where numOne resides instead of the value inside the variable. You want to print numOne with no & operator.
On the other hand, &ansOne is correct for scanf.

Related

VS Code is showing weird numbers before the output? How to fix this?

When running my code in VS Code, it is showing a random weird number in the output:
#include <stdio.h>
void main()
{
int a;
printf("Enter a no: ");
scanf("%d", &a);
if (a%2==0) {
printf("%d It is even no");
}
else{
printf("%d It is odd no");
}
}
As Gino's said, the problem is that you have a %d at the beginning of each of your printf() calls. This tells printf() to pull a number from its argument list. Since you didn't provide a number in the argument list, it pulls the next number on the stack, whatever that happens to be. It's probably a local variable.

Having a problem with a function in my C program. Program terminates before executing the second last "printf()" in the "encript()" function

When I call "encript()", it works how I want it to until the second last "printf()" at which point the program terminates without executing any more lines. I tried searching for similar questions here and tried to fix my code but nothing that I could find seemed to work.
I am a beginner so please forgive me if it's just a simple mistake.
char encript(int x){
printf("You selected Encription with the key: %d\n\n", x); //Tells user the key value they input
Sleep(1000);
int msglength = 100; //Variable for the amount of characters in the messge
printf("\n\nEnter aproximate number of characters in you message. \nThe number must be at least 1 over the amount of characters in your message: ");
scanf("%d", msglength); //assigns user input integer to msglength
printf("\n\n");
char message[msglength]; //Creates a character array for the message with a length of "msglength"
printf("Insert message here: "); ////THIS LINE DOES NOT SEEM TO EXECUTE. NOR DO THE TWO BELOW IT.
scanf("%s", message);
printf("\n\n%s", message);
}
Thanks!
pay attention to this line scanf("%d", msglength); , for scanning variables you should send their address to scnaf , so this should be scanf("%d", &msglength);(add & to scanf)
this function should return a character ,if you just want to to scan a message and print it , you should use void like this void encript(int x) also if you want to return message you can use char * encript(int x) .

Why getting random outputs, in place of input taken in C?

This is C. So trying to put multiple inputs into this array and keep getting numbers like this for when output the input 6356740 "6356744 6356748 6356752"
#include <stdio.h>
#include <stdlib.h>
int main()
{
int quiz[4];
printf("Enter four quiz grades: ");
scanf("%d %d %d %d", &quiz[1], &quiz[2], &quiz[3], &quiz[4]);
printf("%d %d %d %d", &quiz[1], &quiz[2], &quiz[3], &quiz[4]);
}
An array with K elements is indexed from 0 to K-1.
(This is mentioned in any introduction to arrays.)
Reading or writing quiz[4] has undefined behaviour.
You should not pass pointers to printf if you want to print integers.
int main()
{
int quiz[4];
printf("Enter four quiz grades: ");
scanf("%d %d %d %d", &quiz[0], &quiz[1], &quiz[2], &quiz[3]);
printf("%d %d %d %d", quiz[0], quiz[1], quiz[2], quiz[3]);
return 0;
}
You defined your array as
int quiz[4] which means the first reference is quiz[0], the second is quiz[1], the third is quiz[2] and the last is quiz[3].
In your code you are referencing the 2nd, 3rd, 4th and undefined 5th entry. This is important as you may be corrupting the data in your program which can caused undefined results.
The numbers you are seeing are the integer values of the addresses pointing to the ints in your array. Notice they increase by 4 which is the size of an int.
Also, your printf is printing the addresses of the int and not the ints themselves. The line should read
printf(ā€œ%d %d %d %dā€, quiz[0], quiz[1], quiz[2], quiz[3]);
If the grades being input are numbers, that '%d' in all four places is okay. But if you are entering alphabets, than you should be using '%s' instead.
And as Hogstrom and molbdnilo said, the first entry of a Array is 0, than second is 1, and so on. Hence you have to use &quiz[0] than &quiz[1] .. and so on.

What did I do wrong? Trying to get user to enter numeric values for different variables

I am trying to figure out how I can make my program read what the user is typing in, such as a numeric value in this case but I'm having trouble. Here is what I put so far. The first part of the code is just a fake name and email address that just outputs my credentials (so that shouldn't matter).
#include <stdio.h>
int main(void)
{
printf("John Smith\n");
printf("abc#abc.com\n");
/* User must enter values in feet */
int d;
printf("Enter length for side a: \n");
scanf(" %d");
return 0;
}
You forgot to put your variable into the scanf:
scanf(" %d", &d);
printf("You entered %d\n", d);
To read a value from scanf you must give it the address of the variable using (&)
Like so:
scanf(" %d",&d);

Adding numbers until a negative is encountered

I'm trying to create a program that lets the user enter numbers(maximum entries>10^6) until a negative is encountered. I've tried a lot of version but they either don't register that a negative value is entered or they crash.
This is where I'm currently at:
#include <stdio.h>
#define HIGHEST 999999
int main(){
int i=0, entry, sum=0;
while(i<HIGHEST){
scanf("%i", entry);
if(entry>0){
sum+=entry;
}
else{
i=HIGHEST;
}
i++;
}
printf("Sum: %i", sum);
system("pause");
}
Your problem is on this line:
scanf("%i", entry);
Which should be:
scanf("%i", &entry);
You need to pass in the address of the integer variable that will store the scanned value. Since
entry was never initialized, it is just filled with garbage/whatever is in memory and not the entered value. See this reference, which states,
"Depending on the format string, the function may expect a sequence of additional arguments,
each containing a pointer to allocated storage where the interpretation of the extracted
characters is stored with the appropriate type"
You provide a way to leave if the entered number is too big:
while(i<HIGHEST){
But nothing to leave if it is less than 0; Try this:
while((i<HIGHEST)&&(i>=0)){
Additionally, #OldProgrammer is correct, your scanf() should be as he has pointed out.

Resources