C program stops in the middle of running - c

Hi im new here also im new to programming and id like you to help me on this : to problem is that after compiling and running the program it stops in the middle of it when running and i didnt know what is causing this and sorry for the unreadable previous post :
here is my program :
char answer[15];
char place[15];
char fullname[15];
int age;
printf("What Is Your Full Name?: ");
scanf("%s",fullname);
printf("What Is Your Age?: ");
scanf("%d",age);
printf("Where Do You Live?: ");
scanf("%s",place);
if(strcmp(place,"gafsa")==0) {
printf("Aint a bad place you know");
}
else{
printf("hmmm %s cool\n",place);
}
printf("your name is %s, %d year old from %s is that right?: ",fullname,age,place);
scanf("%s",answer);
if(strcmp(answer,"yes")==0){
printf("you my friend are awesome\n");
}
else{
printf("you suck\n");
}
and this is an image to show the problem clearly:
http://i.stack.imgur.com/yFTwK.png

You need to pass the address of the variable:
scanf("%d",&age);
^

You're taking input at a memory location of value of uninitialized age. i.e. some garbage
Use:
scanf("%d",&age); // notice & , pass address of variable age

Related

C LANGUAGE >NOOB HERE< what did i do wrong?

for(a=0;a<99;a++){
reg:
system("cls");
printf("\t\t\t~~REGISTER AN ACCOUNT~~\n\n");
printf("\tDesired Username: ");
scanf("%s", &user[a].user);
for(b=0;b<=a-1;b++){
if(strcmp(user[a].user,user[b].user)==0){
printf("USERNAME IS ALREADY TAKEN");
system("pause");
goto reg;
}
}
printf("\tDesired Password: ");
scanf("%s", &user[a].pass);
printf("\tPersonal/Company Name: ");
scanf("%s", &user[a].name);
printf("\tAddress/Location: ");
scanf("%s", &user[a].address);
printf("\tEmail-Address: ");
scanf("%s", &user[a].email);
printf("\tContact Number: 09");
scanf("%d", &user[a].contact);
break;
}
whenever I put the break in the end the system doesn't recognise if the username is taken or not, but when 'break' is removed it can recognise it.. idk how is this happening.. please help..
PS. sorry if this looks or sounds really stupid to you I am just starting to learn to program(I m trying to make a register program with a very limited knowledge for a school project)
I think the problem lies in your for loops:
First iteration:
a = 0;
for(b=0;b<=a-1;b++)
will not be true
as b<=a-1 equates to b<=-1 which is not true. Hence it will not enter the for loop for b. It will go to break and exit
Same process will repeat until a>=2,
When you don't have break , b waits for b>=2 and starts comparing. That's why you see result of comparison.
A simple check would be to print out values of a and b at every iteration.

undefined symbol st (student record system) in C

When I try to run the following code, it's generating an error:
undefined symbol st
It's showing me an error on option 2 when I try to display full student records.
I'm running it on turbo C++ compiler.
void main()
{
int option, i;
while (5)
{
printf("========== Student Database ==========\n");
printf("--------------------------------------\n");
printf("1. Insert Record\n");
printf("2. Display Record\n");
printf("3. Edit/Update Record\n");
printf("4. Delete a Record\n");
printf("5. Exit\n");
printf("--------------------------------------\n");
printf("Enter Your Choice: ");
scanf("%d",&option);
if(option==1)
{
struct student st[9];
{
printf("\student data");
}
clrscr();
break;
}
else if(option==2)
{
printf("\n===== Displaying Student Information =====\n");
printf("\n Roll No: \t Name \t \t \t Marks \t Mobile Number\n");
for (i = 0; i < 9; ++i)
{
printf("\n %d \t %st \t \t \t %d \t %d\n", st[i].roll, st[i].name, st[i].marks, st[i].number);
}
clrscr();
break;
}
getch();
}
The problem is that your declaration is in the wrong place.
if(option==1)
{
struct student st[9];
...
}
This declaration is only visible inside the if(option==1) clause, but you try and use it inside else if(option == 2)
I'm guessing that you should move the declaration to the start of your program
void main()
{
int option, i;
struct student st[9];
You should read about a couple of concepts that are important when you use variables, scope which is the area of your program where the variable is visiable, and extent which is the time for which you variable exists. Both were wrong in the code you wrote.
There are lots of other errors in your code, but I guess you'll find out about those as you go a long.
struct student st[9]; is confined to the scope of option equal to 1, so st is out scope in the other parts of the if block, hence the compiler diagnostic.
Declare it at the start of main, as you do for option.
Lastly, think about migrating from a Turbo compiler: The standards have moved on considerably since then, and you're only getting yourself into bad habits.
struct student st[9]; is a local variable in the if blocks, that is not available in else block and you try to use it. Move the declaration above the if to make st array available in the both blocks.
It's because of the scope of st. In your code the variable is only valid inside the if block, i.e. it's not available in the else block. Therefore you get a compile error.
Try this instead:
struct student st[9]; // Declare outside the if
if(option==1)
{
// struct student st[9]; Don't do it inside the if

Running C program returns -1.#QNAN0 instead of number stored in floating-point variable

Before going on, I'd like to say that this is my first time here and I don't know how things work yet so please pardon any errors on my part.
When compiled,(source code below) everything works fine except for the content of the float disp which is equal to -1.#QNAN0. Any help on this? Thanks in advance. Some parts of the code are not complete like the switch-case structure. Please temporarily that(Unless it affects the result).
The source code for the C program:
#include <stdio.h>
#include <stdlib.h>
float moneyup(float m);
int main()
{
char name[20];
char x;
int y;
float disp;
int hunger;
printf("\t\t**********************************************\n");
printf("\t\t* *\n");
printf("\t\t* How To Get Rich Quick! *\n");
printf("\t\t* *\n");
printf("\t\t**********************************************\n");
printf("\nThis is an experimental command line interface game made by NayNay AKA Nathan\n");
printf("\nPlease pardon the poor user interface.");
for(;;)
{
printf("\nPlease enter your name(one only)");
scanf("%s", &name);
printf("\nThe name you entered is %s. Is this correct? (type y/n for yes or no)\n");
fflush(stdin);
x=getchar();
if(x=='y') /*This part with the for loop is used to get the name of the*/
{ /*user and confirm the correctness of that name. If the name is*/
printf("Okay! Moving on..."); /*wrong, the user has the option to change it. Bulletproofing used*/
break; /*here*/
}
else if(x=='n')
{
printf("Alright let's try again.");
continue;
}
else
{
printf("Let's try this again.");
continue;
}
}
printf("\nOkay %s, Let's get this story started",name);
printf("\n\nOne sad dreary morning, %s got up from sleep and went to the kitchen to get breakfast.");
printf("\nUnfortunately for him his pantry only contained a bunch of cockroaches going at it and laying their eggs everywhere");
printf("\nHe then checked his pockets and pulled out his last 5-dollar bill. That was all he had left,");
printf("\nHe bought a sandwich for $2 and decides to start a business with $3 as capital");
printf("\n\nChoose how to start");
printf("\n1. Begging.");
printf("\n2. Mow lawns.");
printf("\n3. Apply for post of newspaper boy.");
fflush(stdin);
y=getchar();
switch(y)
{
case '1':
printf("You begged for 6 hours and got $5.25\n");
disp=moneyup(5.25);
printf("You now have $%f\n",disp);
}
return 0;
}
float moneyup(float m)
{
float money;
money=(float)money+m;
return(money);
}
The variable money is uninitialized in the function moneyup when used in expression
money=(float)money+m;

Simple Multichoice, Multivariable, Calculator Query

I recently programmed this code (C) for a pretty simple calculator in Xcode.
It works mostly but does not display the answer for the sum/s.
The code is as follows:
#include <stdio.h>
int main()
{
//int's & chars-----------------
char SumMethod;
int firstnumber, secondnumber;
//int's & chars------------------
puts("Calculator v0.6");
printf("Please input first number: "); //Prompts 'firstnumber' input
scanf("%d", &firstnumber); //Scan's 'firstnumber' input and saves to '&firstnumber'
printf("Please input second number: "); //Prompts 'secondnumber' input
scanf("%d", &secondnumber); //Scan's 'secondnumber' input and saves to '&secondnumber
printf("d" "Please select Method: +(a), -(b), *(c), /(d): ");
scanf("%c", &SumMethod); //Scan's 'SumMethod' input
if(SumMethod=='a') {
printf ("%d",firstnumber + secondnumber); //This section detects the SumMethod and outputs the corrisponding sum
}
else if (SumMethod=='b'){
printf("%d",firstnumber-secondnumber);
}
else if (SumMethod=='c') {
printf("%d",firstnumber * secondnumber);
}
else if (SumMethod=='d') {
printf("%d",firstnumber / secondnumber);
}
}
For the solution i'm looking for a response that doesn't drastically change the code as how it is now is what I understand and because i'm relatively new to coding in general however if there is an obvious fix using useful common tools that would be much appreciated.
Thanks, Xenon
Your problem is that your comparisons are wrong. You need to compare your input to the LETTER a and not the (undefined and essentially random) values of the variables a,b,c,d.
so it is
if(SumMethod=='a')
instead of
if(SumMethod==a)
You don't need your variables a,b,c,d at all.

My program crashes, and I don't understand why

I am coding a record-keeping program in C using binary file handling. I am using Code::Blocks, with gcc to compile my C program on Windows 8.
When the program reaches to the following block, an error-message appears:
My code:
int dispaly(student record[], int count)
{
/*
This is what structure `student` looks like:
int id;
char name[200], phone[20], address[200], cclass[50];
char sec[20], roll[50], guardian_name[200], relation[200] ;
char p2_colg[100], slc_school[200];
float plus2_percent, slc_percent;
struct date dob;
struct date enr_date;
struct date looks like
int day, month, year;
*/
printf("Reached"); /*Program Runs Fine upto here*/
int i = 0;
for(i=0; i<count; i++)
{
printf("\nId: %d\tPhone: %s\nName: %s\nAddress: %s"
"\nClass: %s\tSection: %s\nRoll: %s\nGuardian Name: %s\tRelation:%s"
"\nPlus-Two in: %s\tPercentage:%f\nSLC School: %s\tPercentage: %f"
"\nDate Of Birth(mm/dd/yyyy): %d/%d/%d"
"\nEnrolled in (mm/dd/yyyy): %d/%d/%d\n\n---------------------------------------\n", record[i].id, record[i].name, record[i].address
, record[i].cclass, record[i].sec, record[i].roll, record[i].guardian_name, record[i].relation, record[i].p2_colg
, record[i].plus2_percent, record[i].slc_school, record[i].slc_percent, record[i].dob.month, record[i].dob.day, record[i].dob.year
, record[i].enr_date.month, record[i].enr_date.day, record[i].enr_date.year);
}
getch();
return 0;
}
The program compiles without any errors or warnings.
What's going on?
Hard to tell exactly what crashed without looking at the exact data in your array, but you forgot "phone" in the arguments list to printf, which could certainly result in a crash inside printf.
There's not a terribly good reason to stack those all up into one call. It would have been easier to spot your bug of the missing "phone" if you separated each line out into its own printf. Also, you could cut down on the redundancy if you captured record[i] into a pointer.
Contrast with:
student * r = &record[i];
printf("\n");
printf("Id: %d\tPhone: %s\n", r->id, r->phone);
printf("Name: %s\n", r->name);
printf("Address: %s\n", r->address);
printf("Class: %s\tSection: %s\n", r->cclass, r->sec);
printf("Roll: %s\n", r->roll);
printf("Guardian Name: %s\tRelation:%s\n", r->guardian_name, r->relation);
printf("Plus-Two in: %s\tPercentage:%f\n", r->p2_colg, r->plus2_percent);
printf("SLC School: %s\tPercentage: %f\n", r->slc_school, r->slc_percent);
printf("Date Of Birth(mm/dd/yyyy): %d/%d/%d\n",
r->dob.month, r->dob.day, r->dob.year);
printf("Enrolled in (mm/dd/yyyy): %d/%d/%d\n"
r->enr_date.month, r->enr_date.day, r->enr_date.year);
printf("\n");
printf("---------------------------------------\n");
In a technical sense, making multiple calls to printf will incur some function call overhead. And declaring a pointer variable for the current student in the array will incur some storage space. But it is basically negligible, and of no consequence in a case like this. Under the hood, the output is buffered anyway.

Resources