Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
This is the relevant code, I'm trying to write a function to pick type of account and when I run this, it keeps running in a weird loop. Any solutions?
void AccType(){
wrongInput:;
int typCho;
printf("What type of account do you want to open?\nPress 1 for Current\n2 for savings\n3 for retirement:\n");
scanf("%d",typCho);
switch (typCho) {
case 1:
strcpy(PracRec.AccTyp,"Current");
break;
case 2:
strcpy(PracRec.AccTyp,"Savings");
break;
case 3:
strcpy(PracRec.AccTyp,"Retirement");
break;
default:
printf("Please enter a valid choice!!\n");
goto wrongInput;
}
}
The second argument of scanf needs to be a pointer (memory adress) to an integer...
So, replace the line:
scanf("%d",typCho);
with:
scanf("%d", &typCho);
& is an operator that returns the memory address of the operand at right.
This is needed because scanf is going to change the value inside the memory address which relates to the variable.
When you put only typCho as the argument, it probably made the program to write the input at a random memory location, as the variable was also uninitialized.
Also, consider avoiding the use of goto, as pointed in the previous comments.
You could easily do the same thing with a construct like:
do { ...code... } while (condition);
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 10 months ago.
Improve this question
int n;
scanf("%d",&n); //input from keyboard=5
printf("%d ",n); //gives output 5.
n=scanf("%d",&n); //input from keyboard=5
printf("%d ",n); //gives output 1.
Need help understanding the second one.
scanf returns the number of fields it assigns. So, first, it assigns n as 5. Then it returns 1 as it has assigned to only 1 variable. Then, n takes that value.
That's not the correct way to use scanf(). In C standards scanf() always returns the number of conversion it did from stdin, in your case only 1 conversion is done, hence 1 is assigned to n.
In C, if you want to modify a parameter's value inside a function, you need to pass the address of the variable (&var) to that function, and the parameter of the function should be a pointer (type *). Then, you need to de-reference the pointer like (*ptr).
Now, we need to change the value of n inside scanf() function, that's why we need to give the address of n.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I was thinking of a code to find the sum of numbers.
So i wrote bellow code :
#include <stdio.h>
#include <math.h>
int main()
{
int n,i,s=0,a[100];
printf("enter number of numbers");
scanf("%d",n);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
s=0;
for (i=1;i<=n;i++)
{s=s+a[i];}
printf("sum is%d\n",s);
}
But in the output it shows segmentation error . I.e.
So whats the mistake?
So this line:
scanf("%d",n);
should be replaced by
scanf("%d",&n);
Explanation:
scanf() reads data from stdin according to format and store data in the location pointed by next additional argument. this case, format is %d means we want to read an integer number and this integer number will be stored in the location of n. The & operator is to get the location of a variable in C.
Use this :
scanf("%d",&n);
The reason :
You MUST put & in front of the variable used in scanf. The reason why will become clear once you learn about pointers. It is easy to forget the & sign, and when you forget it your program will almost always crash when you run it.
Examples :
scanf("%d %d", &a, &b);
printf("%d %d", a, b);
As a and b above are two variable and each has their own address assigned but instead of a and b, we send the address of a and b respectively. The reason is, scanf() needs to modify values of a and b and but they are local to scanf(). So in order to reflect changes in the variable a and b of the main function, we need to pass addresses of them. We cannot simply pass them by value.
But in case of printf function as we are only going to print the values of the variables in output console, there are no changes going to be made in variable a and b’s values. So it is not required to send their addresses.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I did the exact same thing in another part of code and got no error or any problem with it's way of working but in this part and so it gives me an error.
So the function is;
void triangle(int height,int base)
{
printf("Enter the base of the triangle:");
scanf("%d",&base);
printf("\nEnter the height of the triangle:");
scanf("%d",&height);
int area;
area = (height*base)/2;
printf("\nTriangles area is %d.",area);
}
I'm using a switch-case function for my code and this is how I call it in main function;
case 3:
{
triangle(area);
}
Feels weird but this works perfectly;
void square(int length)
{
printf("Enter the length of square:");
scanf("%d",&length);
int area;
area = length*length;
printf("\nRectangles area is %d.",area);
}
Like this;
case 1:
{
square(area);
}
I did the exact same thing in another part of code and got no error or
any problem with it's way of working but in this part and so it gives
me an error.
C does not guarantee to diagnose all incorrect code, neither at compile time nor at run time. Nevertheless, you should turn up your compiler's warning level. If afterward it anywhere accepts a call to your triangle() function with other than exactly two arguments and without emitting at least a warning of some kind, then throw it out and choose a better one.
So the function is;
void triangle(int height,int base)
{
printf("Enter the base of the triangle:");
scanf("%d",&base);
printf("\nEnter the height of the triangle:");
scanf("%d",&height);
int area;
area = (height*base)/2;
printf("\nTriangles area is %d.",area);
}
Note that your function does not actually use the value provided by the caller for either argument, nor does it in any way return any information to its caller, even if you call it with the correct number of arguments. If that's what you want then it would be better to declare it to accept no arguments, and to call it that way:
void triangle(void) {
int length;
int height;
// ...
Note that length and height are now strictly local variables. You call that variation like so:
triangle();
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I need to memorize how much memory I allocated with realloc().
Help me
if(!array)
array=(Type*) calloc(1,sizeof(Type));
else
array=(Type*)realloc(array,(cont+1)*sizeof(Type));
array[cont].setName(....);
cont++;
It doesn't work: after firt insert, it say: Access violation
I initialized the cont = 0 in the constructor of my class and freed memory in the destructor.
See the comments added to your code:
int count=0;
if(!array)
array=(Type*) calloc(count,sizeof(Type*); // Problem:
// missing )
// use sizeof(Type)
// calling calloc with count being zero
// so you do not allocate any memory
// use 1 instead of count
array[c].setName(EditName->Text);
c++;
count++;
array=(Type*)realloc(array,count*sizeof(Type*)); // Problem:
// use sizeof(Type)
so it should look:
int count=0;
if(!array)
array=(Type*) calloc(1,sizeof(Type));
array[c].setName(EditName->Text);
c++;
count++;
array=(Type*)realloc(array,count*sizeof(Type));
The variable c must be initialized to zero before running this code
Likewise array must be nullptr before running this code
EDIT
There seem to be one more problem if you intend to run this code several times (which I assume you do).
This line:
array=(Type*)realloc(array,count*sizeof(Type));
^^^^^
Don't use count here as you always sets count to zero
The line shall be:
array=(Type*)realloc(array,c*sizeof(Type));
In general there seems to be no real use of count
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have this code that will print numbers from 0 to 9 in english words (like one for 1, two for 2, etc.). What if I wanted to print 374? Or something much larger, like 7549846451?
#include <stdio.h>
int main()
{
double sum;
if(scanf("%1f",&num)!=0)
{
if(num=(int)num)
{
switch((int)sum)
{
case 0:printf("zero\n");break;
case 1:printf("one\n");break;
case 2:printf("two\n");break;
case 3:printf("three\n");break;
case 4:printf("four\n");break;
case 5:printf("five\n");break;
case 6:printf("six\n");break;
case 7:printf("seven\n");break;
case 8:printf("eight\n");break;
case 9:printf("nine\n");break;
default:printf("not a digit"); break;
}
}else
{
printf("Invalid")
return 0;
}
}
return 0;
}
This is a good start, but it would take a lot more to complete your program:
Start by expanding your code to printing numbers 10..99. There would be a special case for 11..19, but after that it's pretty regular. The lower 20 can be addressed with a lookup table. In fact, making a look-up table for the whole range wouldn't be too bad, either.
With a routine that writes out numbers 0..99 in hand you can expand into hundreds by looking at the third digit the right, writing it out, adding "hundred", and proceeding to writing out the number 0..99
Now that you have a routine for writing out three-digit numbers all you need is to split your number into groups of tree, calling this routine for non-zero groups, and adding "billion", "million", and "thousand" corresponding to the rank of the group.
Here you have the solution to your problem. It is even the same example as you have pasted here, so if you have read the comments below, you'd have seen the comment form Bheema in which he posted the whole code for it.
Also, you can try writing your own code, it's not that hard. dasblinkenlight gave you instructions how to do it.