Crashes before last loop - c

This is a relatively simple program for tabulating grades.
My program crashes while trying to complete the last loop, specifically at the last midterm input. Any help here?
#include <stdio.h>
#include <string.h>
#define ARRAYSIZE 2
int main(void)
{
char studentID[ARRAYSIZE][10];
int midterm[ARRAYSIZE];
int fina[ARRAYSIZE];
int i=0;
double overall[ARRAYSIZE];
for (i=0;i<ARRAYSIZE;i++)
{
printf("\nInput Student ID:");
scanf("%s",&studentID[i][10]);
printf("\nInput midterm score:");
scanf("%d",&midterm[i]);
printf("\nInput final score:");
scanf("%d",&fina[i]);
overall[i]=midterm[i]*0.3+fina[i]*0.7;
}
printf("\nStudent ID MidTerm Final Overall\n");
for (i=0;i<ARRAYSIZE;i++)
{
printf("%s%5d%5d%5f",studentID[i][10],midterm[i],fina[i],overall[i]);
}
return 0;
}
The process returned is -1073741819 (0xC0000005). Thanks.

The error is in the final printf statement, specifically 'studentID[i][10]' here you are accessing an element at an illegal index (0 to 9 are legal in your case)

please try this
//declaration
int ARRAYSIZE=2;
char studentID[ARRAYSIZE][10];
//Then, you need to enter the string into the array
int i;
for (i=0; i<ARRAYSIZE; i++) {
scanf ("%s" , studentID[i]);
}
// in oreder to print them use
for (i=0; i<ARRAYSIZE; i++) {
printf ("%s" , studentID[i]);
}

Related

Why does my program exit after taking an input?

Consider:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n;
char name[100];
int number;
printf("Enter the value of n\n");
scanf("%d",&n);
printf("Enter %d values\n", n);
for(int i=0; i<n; i++)
{
scanf("%[^\n]s", &name);
}
}
Whenever I am entering the value of n, it just prints (Enter n values) and exits the program. The for loop never runs. It ran successfully for the first time, but after that it just exits the program.
There were some answers that said it will not print anything. I don’t want it to print just to take input n times. It is not doing that.
My aim is to take n as input and then take strings of names (like harry, robin, etc.) n number of times as input.
Your code is a little incomplete. And there are a few errors here: scanf ("%[^\n]s", &name)
Do this and everything will be fine:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main(void)
{
int n;
char name[100];
int number;
printf("Enter the value of n\n");
scanf(" %d", &n);
printf("Enter %d values\n", n);
for(int i=0; i<n; i++)
{
scanf(" %99[^\n]", name);
printf("%s\n", name);
}
return 0;
}
scanf is particularly unsuited for user input.
You probably want this:
int main() {
int n;
char name[100];
int number;
printf("Enter the value of n\n");
scanf("%d", &n);
printf("Enter %d values\n", n);
for (int i = 0; i < n; i++)
{
// the space at the beginning of "%[^\n]"
// gets rid of the \n which stays in the input buffer
scanf(" %[^\n]", name); // also there sis no 's' at the end of the "%[^\n]" specifier
printf("name = %s\n", name); // for testing purposes
}
}
But this doesn't actually make much sense because the program is asking for n names, but at each run of the for loop the previous name will be overwritten with the new name.
Also be aware that scanf("%[^\n]", name); is problematic because if the user types more than 99 characters you'll get a buffer overflow.

How do you use scanf in a for loop so it does not stop looping on the first try?

for(int i=0;i<num; i++)
{
char word[32];
scanf(" %[^\n]s",word);
makeDictionary(word, readDictionary);
}
I have a program where I want to ask user for certain strings n times (with spacing allowed). However, when they input for example n = 2, it only loops once and exists. I know there is something wrong with my scanf.
I do Java and I'm a beginner at C. The way strings are done is very different.
This code can help you:
#include <stdio.h>
#include <string.h>
int main()
{
int N;
do
{
printf("Give me number of words :");
scanf("%d",&N);
}while(N<1);
for(int i=0;i<N;i++)
{char str[20];
printf("\nGive me the %d word :",i+1);
scanf("%s[^\n]",str);
printf("%s",str);
}
}

Program stops working after entering the value for more than 6 users

I have a school project which asks me to write a code using struct and functions, regarding sales in Greek cities. You are prompted to enter the values of 'em. The program works just fine if I enter that there's 6 salesmen, but a number larger than that makes it stop after entering all the values.. An example would be:
Number of salesmen: 7
(
Enter the his id:
Enter his surname:
Enter the number of sales:
Enter the area code:
) x7 times.
Then, when you enter the last value, it would just stop. Not exit, not stop responding/crash, literally just stop, and you would be unable to type anything more, only option would be to exit.
Thing is, there's no error or warning in both the build messages and the log, which has me quite confused.
My guess is that the error is in the function "calcSales" but I wanted to post the whole code just in case you need more info on it.
Could you take a look at the code and tell me if you find anything wrong? Thank you.
#include <stdio.h>
#include "genlib.h"
#include <simpio.h>
#include <string.h>
#define N 20
#define M 4
struct{
int id;
char surname[16];
long sales;
int area;
} salesmen[N];
void info(int *count);
void calcSales(int *count);
int main(){
int count;
printf("Give me the number of salesmen:\n");
count=GetInteger();
info(&count);
calcSales(&count);
}
void info(int *count){
for (int i=0; i<*count; i++){
printf("\nInfo for salesman number %d:\n", i+1);
printf("\nGive me his id: ");
salesmen[i].id=GetInteger();
printf("\nGive me his surname: ");
gets(salesmen[i].surname);
printf("\nGive me the number of sales: ");
salesmen[i].sales=GetLong();
printf("\n 1=Thessaloniki, 2= Athens, 3= Volos, 4= Hrakleio \n");
printf("\nLastly, give me the number of his area: ");
salesmen[i].area=GetInteger();
if (salesmen[i].area>4){
printf("\nThe number you are trying to enter doesn't match to an area.\n");
break;
}
}
}
void calcSales(int *count){
long tSales[4];
for (int i=0; i<*count; i++){
tSales[i]=0;
}
for (int i=0; i<*count; i++){
if(salesmen[i].area==1){
tSales[0]+=salesmen[i].sales;
}
if(salesmen[i].area==2){
tSales[1]+=salesmen[i].sales;
}
if(salesmen[i].area==3){
tSales[2]+=salesmen[i].sales;
}
if(salesmen[i].area==4){
tSales[3]+=salesmen[i].sales;
}
}
for (int i=0; i<4; i++){
printf("\nSales for area number %d: %ld\n",i+1, tSales[i]);
}
}
Your guess is correct. The mistake in calcSales function.
Exactly, the following part:
long tSales[4];
for (int i=0; i<*count; i++) {
tSales[i]=0;
}
The *count has value of 20. Which means the loop goes from i = 0 until i = 19.
When you access tSales[i] for i = 4 and above. You're invoking an undefined behavior. It's a memory that you didn't reserve for tSales. I suggest using the following:
long tSales[4];
for (int i=0; i<4; i++) {
tSales[i]=0;
}
Or better:
long tSales[4];
for (int i=0; i<sizeof(tSales)/sizeof(tSales[0]); i++) { // number of elements is the total size of array divided by the size of one element.
tSales[i]=0;
}
Or even better, you don't need a loop at all:
long tSales[4] = {0};
Besides your problem:
For the code you're providing, you don't need to pass a pointer to the functions.
You can make the code look as follows:
#include <stdio.h>
#include "genlib.h"
#include <simpio.h>
#include <string.h>
#define N 20
#define M 4
struct{
int id;
char surname[16];
long sales;
int area;
} salesmen[N];
void info(int count);
void calcSales(int count);
int main(){
printf("Give me the number of salesmen:\n");
int count=GetInteger(); // I've made declaration and assignment in same line. That seems cleaner to me.
info(count);
calcSales(count);
}
void info(int count){
for (int i=0; i<count; i++){
printf("\nInfo for salesman number %d:\n", i+1);
printf("\nGive me his id: ");
salesmen[i].id=GetInteger();
printf("\nGive me his surname: ");
gets(salesmen[i].surname);
printf("\nGive me the number of sales: ");
salesmen[i].sales=GetLong();
printf("\n 1=Thessaloniki, 2= Athens, 3= Volos, 4= Hrakleio \n");
printf("\nLastly, give me the number of his area: ");
salesmen[i].area=GetInteger();
if (salesmen[i].area>4 || salesmen[i].area < 1){ // Added an extra condition.
printf("\nThe number you are trying to enter doesn't match to an area.\n");
break;
}
}
}
void calcSales(int count){
long tSales[4] = {0};
for (int i=0; i<count; i++){
tSales[salesmen[i].area - 1]+=salesmen[i].sales; // No need for if conditions.
}
for (int i=0; i<4; i++){
printf("\nSales for area number %d: %ld\n",i+1, tSales[i]);
}
}

How to allot sections in a structure member by taking subject from user in as another Structure member in c language

/*Take value and store in structure member, compare with constant
do batch allotment in it.*/
#include <stdio.h>
#include <conio.h>
void main()
{
struct student{
float mrk;
char sub[10];
char sec[10];
} st[2];
int i;
float *p=&st[i].mrk;
clrscr();
for (i=0; i<2; i++)
{
printf("Enter 12th mrks: ");
scanf("%f", &st[i].mrk);
fflush(stdin);
printf("Enter subject: ");
gets(st[i].sub);
}
for (i=0; i<2; i++)
{
if (st[i].sub=="cse")
strcpy(st[i].sec,"A"); //st[i].sec="A";
else if (st[i].sub=="me")
strcpy(st[i].sec,"B"); //st[i].sec="B";
else
strcpy(st[i].sec,NULL); //st[i].sec=NULL;
}
printf("\n numbers \t sub \tsec \n");
printf("-------------------------------------\n");
for (i=0; i<2; i++)
{
printf("%f\t%s\t%s\n",st[i].mrk,st[i].sub,st[i].sec);
}
getch();
}
Hello. This is the code. Please help. I'm unable to get the output for alloting sections according to the subject taken from user. It prints nothing over there and there is no error in turbo c7 compiler that I'm working on.

How to get multiple inputs in one line in C?

I know that I can use
scanf("%d %d %d",&a,&b,&c):
But what if the user first determines how many input there'd be in the line?
You are reading the number of inputs and then repeatedly (in a loop) read each input, eg:
#include <stdio.h>
#include <stdlib.h>
int main(int ac, char **av)
{
int numInputs;
int *input;
printf("Total number of inputs: ");
scanf("%d", &numInputs);
input = malloc(numInputs * sizeof(int));
for (int i=0; i < numInputs; i++)
{
printf("Input #%d: ", i+1);
scanf("%d", &input[i]);
}
// Do Stuff, for example print them:
for (int i=0; i < numInputs; i++)
{
printf("Input #%d = %d\n", i+1, input[i]);
}
free(input);
}
Read in the whole line, then use a loop to parse out what you need.
To get you started:
1) Here is the manual page for getline(3):
http://man7.org/linux/man-pages/man3/getline.3.html
2) Some alternatves to getline:
How to read a line from the console in C?
3) Consider compressing spaces:
How do I replace multiple spaces with a single space?
4) Use a loop for parsing. You might consider tokenizing:
Tokenizing strings in C
5) Be careful and remember that your user could enter anything.
#include <conio.h>
#include <stdio.h>
main()
{
int a[100],i,n_input,inputs;
printf("Enter the number of inputs");
scanf("%d",&n_input);
for(i=0;i<n_input;i++)
{
printf("Input #%d: ",i+1);
scanf("%d",&a[i]);
}
for(i=0;i<n_input;i++)
{
printf("\nInput #%d: %d ",i+1,a[i]);
}
}
/*
_______________This program is in C Programming Language_______________
We have to directly enter all the elements in one line giving spaces between them. Compiler will automatically ends the for loop I have used and assign the value to their respective variables or array indexes. Below program and output will give you better understanding.
*/
#include <stdio.h>
int main()
{
//taking no of inputs from user
int len;
printf("Enter the number of inputs you want to enter : ");
scanf("%d", &len);
int i;
//defined an array for storing multiple outputs
int arr[100];
//included a printf statement for better understanding of end user
printf("Enter the inputs here by giving space after each input : ");
/*here is the important lines of codess for taking multiple inputs on one line*/
for (i=0;i<len;i++)
{
scanf("%d", &arr[i]);
}
printf("Your entered elements is : ");
for (i=0;i<len;i++)
{
printf("%d ", arr[i]);
}
}
/*
OUTPUT :
Enter the number of inputs you want to enter : 5
5 5 5 8 7
Your entered elements is : 5 5 5 8 7
*/

Resources