C programming Calc - c

I am trying to make a simple calculator in c as i want to test my programming skills. I keep getting a error though.
#include <stdio.h>
int calc()
{
int *fnum;
int *snum;
printf("Enter your First Number: ");
scanf("%d", fnum);
printf("Enter your Second Number: ");
scanf("%d", snum);
int answer = *fnum + *snum;
printf("%d", answer);
return 0;
}
int main()
{
int *calcType;
printf("Type of Calculation: 1=A, 2=S, 3=M, 4=D: ");
scanf("%d", calcType);
if (*calcType == 1)
{
calc();
}
return 0;
}
But Then i get this error:
Segmentation fault (core dumped)
Please help, i have no idea what this means.

You called scanf to read an integer into the memory pointed to by calcType, but you never set calcType to point to a valid address.

Where the snum and fnum (int * pointers) point to? you have to declare a variable and pass its address (by reference operator &) to scanf.
The code should be something like this
int calc(){
int fnum;
int snum;
printf("Enter your First Number: ");
scanf("%d", &fnum);
printf("Enter your Second Number: ");
scanf("%d", &snum);
int answer = fnum + snum;
printf("%d", answer);
return 0;
}
Also same problem with calcType pointer.

You should be inputing ints, not int* (int pointers):
int fnum; /* This is now an int */
int snum; /* so is this */
printf("Enter your First Number: ");
scanf("%d", &fnum); /* Note that fnum's address is passed */
printf("Enter your Second Number: ");
scanf("%d", &snum); /* Same for snum */
int answer = fnum + snum;
printf("%d", answer);
return 0;

That is a generic error message. You are incorrectly using pointers for fnum and snum, and attempting to add their addresses in memory.

You missed
&
Without it the programm wont know where to storage the data
scanf("%d", &fnum);
I also dont understand why you put
*

Related

Why am I getting garbage value here?

Its a program to convert integer stored in a string into an int. Please help.
#include<stdio.h>
#include<math.h>
int main()
{
int num, n, i;
num = 0;
printf("Enter n\n");
scanf("%d", &n);
char string[n+1];
printf("Enter the number\n");
scanf("%s", &string);
for(i=0;string[i]!='\0';i++)
{
num = num + string[i]*pow(10,n-i-1);
}
printf("The required number is %d", num);
return 0;
}
In typical environment, character codes for digits are not equal to the numbers the digits represent for.
Character codes for digits in C are defined to be continuous, so you can convert the character codes to the corresponding numbers by subtracting '0' from the character code.
num = num + (string[i]-'0')*pow(10,n-i-1);
By the way, there are some better ways to do the conversion:
sscanf(string, "%d", &num); /* available in stdio.h */
num = atoi(string); /* stdlib.h is required for atoi() */
Also note that scanf("%s", &string); invokes undefined behavior because a pointer to array is passed where char* (a pointer to char) is required. The & should be removed.
In your code the size of your string must be equal to n+1 ,so you can removed all this
printf("Enter n\n");
scanf("%d", &n);
char string[n+1];
for(int i=strlen(string)-1;i>=0;i--)
I started from the last caracter in my string to the first caracter and I multiplied each caracter by j (j=1 then j=10 then j=100 .....)
#include<stdio.h>
#include<math.h>
int main()
{
int j=1;
char string[100];
printf("Enter the number\n");
scanf(" %s", string);
int num=0;
for(int i=strlen(string)-1;i>=0;i--)
{
int k=string[i]-'0';//subtract each case by '0'
num=num+k*j;
j=j*10;
}
printf("The required number is %d", num);
return 0;
}

How to use char in scanf by using loop?

I want to make simple billing software but I don't know to fix this problem
#include <stdio.h>
int main()
{
char a[20];
int i, j, b;
i = 0;
printf("How many item you have?\n>>> ");
scanf("%d", &j);
for (int i = 0; i < j; i++)
{
printf("Type the name of item no. %d?\n>>> ", i + 1);
scanf("%c", &a);
printf("Type the item quantity?\n>>> ");
scanf("%d", &b);
}
return 0;
}
This code is only for asking questions, as you can see. In this code everything is fine but, when I run this code, the output is:
How many item you have?
>>> 4
Type the name of item no. 1?
>>> Type the item quantity?
>>>
Everything seems fine but I haven't entered the item name and the loop is asking the 2nd question directly. How is it even possible?
The %c format specifier for scanf reads a single character. To read a string (array) of characters, use the %s format specifier. Also, for such arrays, you don't need the & (address of) operator, as the array name itself will 'decay' to a pointer to its first element:
#include <stdio.h>
int main()
{
char a[20];
int i, j, b;
i = 0;
printf("How many item you have?\n>>> ");
scanf("%d", &j);
for (int i = 0; i < j; i++) {
printf("Type the name of item no. %d?\n>>> ", i + 1);
scanf("%19s", a); // The "19" limits input size and allows space for the nul-terminator
printf("Type the item quantity?\n>>> ");
scanf("%d", &b);
}
return 0;
}

How do I get input for an array from a user in C programming?

I am new to C and I've run into a bit of a problem when it comes to user input for an array.
Here is the code
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int n, i;
int score [n];
printf("Number of scores: ");
scanf("%d", &n);
for(i=0; i<n; i++){
printf("score: ");
scanf("%d", &score[i]);
}
return 0;
}
It does not matter what value I set for n. It always prompts the user 4 times.
As mentioned in comments, you must change this:
/* bad */
int score [n];
printf("Number of scores: ");
scanf("%d", &n);
into this
/* good */
printf("Number of scores: ");
scanf("%d", &n);
int score [n];
This since C executes code from top to bottom like when you are reading a book. It will not "double back" a few rows above and fill in n once it has been entered by the user. At the point where you declare int score [n], n must already be known.
If your using an array with unknown size during compilation, I would suggest using memory allocation. So the user determines the array size while running the program.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int n, i;
int *score;
printf("Number of scores: ");
scanf("%d", &n);
score = (int *)malloc(sizeof(int)*n);
for(i=0; i<n; i++){
printf("score: ");
scanf("%d", &score[i]);
}
free(score)
return 0;
}
The malloc function allocates memory with the size of n and returns a pointer to the allocated memory.

Referenced memory could not be written

Whenever I run this struct, I can get down to the line where you would input the constitution modifier and the program crashes with a popup window which reads "The instruction at 0x00000000775AFDE9 referenced memory at 0x000000007758D250. The memory could not be written. Press OK to terminate." Here's the struct in question:
struct player_info create_player(void);
struct player_info{
char name[30];
int Level, Str, Dex, Con, Int, Wis, Cha;
};
struct player_info create_player(void){
struct player_info aPlayer;
{
char c;
int i;
printf("Enter Player Name: ");
scanf("%s",aPlayer.name);
i = strlen(aPlayer.name);
do{
scanf("%c", &c);
aPlayer.name[i++] = c;
}
while (c != '\n');
aPlayer.name[i - 1] = 0;
}
printf("Level: ");
scanf("%d",aPlayer.Level);
printf("Strength Modifier: ");
scanf("%d",aPlayer.Str);
printf("Dexterity Modifier: ");
scanf("%d", aPlayer.Dex);
printf("Constitution Modifier: ");
scanf("%d", aPlayer.Con);
printf("Intelligence Modifier: ");
scanf("%d", aPlayer.Int);
printf("Wisdom Modifier: ");
scanf("%d", aPlayer.Wis);
printf("Charisma Modifier: ");
scanf("%d", aPlayer.Cha);
return aPlayer;
};
And the write bit:
int save_data(){
FILE* PlayerFile = fopen("players.txt","w");
int i = 0;
for (i = 0; i < 1; i++){
struct player_info aPlayer = create_player();
fprintf(PlayerFile, "%s %d %d %d %d %d %d %d\n", aPlayer.name, aPlayer.Level, aPlayer.Str, aPlayer.Dex, aPlayer.Con, aPlayer.Int, aPlayer.Wis, aPlayer.Cha);
}
fclose(PlayerFile);
return 0;
}
Now, to be clear, I can input up to the dexterity modifier. The next line that should ask for the constitution doesn't print, and that's when I get the popup error.
I have tried commenting out everything from the constitution mod down to the charisma just to see, and I get the same problem. Removing just the constitution part doesn't work either. I'm not really sure what's going on here; I've seen other posts saying something about a pointer being wrong, but I don't see anything like that, unless it's just one of those things that you just miss and need someone else to point it out. Anyway, any help is appreciated.
scanf expects the address of the variable you intend to write to. So this
scanf("%d",aPlayer.Level);
Should be this
scanf("%d", &aPlayer.Level);
For all of your stats. The way you have it setup now involves passing an unspecified integral value to scanf (the variable aPlayer.Level and company are uninitialized), which is then reinterpreted as an address that the function attempts to write into. The behavior of of such code is undefined.

I am getting an Memory fault(coredump) error message in my c program?

I am not able to understand why it is showing this error.
I have never encountered such an error before.
Here is my code, can you identify the mistake or the cause of it :
#include<stdio.h>
#include<math.h>
void dec2bin(int n,int bin[1000]){
int num = 0, index = 0, i;
while (n != 0){
bin[index] = n%2;
index++;
n = n/2;
}
}
int Sub(int a[100],int b[1000],int ac[100],int siz){
int i=siz-1,k=0;
for(;i>=0;i--){
if(b[i]){
a[k++]=ac[siz-i-1];
}
}
return k;
}
int sum(int a[100],int s){
int i,sum=0;
for(i=0;i<s;i++)
sum+=a[i];
return sum;
}
main(){
int b[1000],sub[100],a[100],n,i,s,count=0;
printf("Enter n: ");
scanf("%d",n);
for(i=0;i<n;i++){
printf("Enter number %d",i+1);
scanf("%d",&a[i]);
}
printf("Enter S: ");
scanf("%d",s);
int no=(int)pow(2.0,(float)n);
for (i=0;i<1000;i++)
b[i]=0;
for(i=0;i<no;i++){
dec2bin(i,b);
int siz=Sub(sub,b,a,n);
if(sum(sub,siz)==s)
count++;
}
printf("Subsets: %d",count);
}
And this code shows memory fault error immediately after entering the value of n.
You're using scanf slightly incorrectly.
The arguments following the format string need to be pointers to your objects, not the objects themselves.
scanf("%d", &n);
Do remember though, that scanf is very dangerous to use. Behavior is undefined for instance if the integer would overflow. Better to read a line safely then use strtol to parse it, since you can detect errors properly.
when taking input for n.
scanf("%d", &n);
You forgot a &

Resources