exited with non-zero status - c

I am new in programming and try to learn by own ,i have an error in my code,i don't understand it's a syntax error or i've done smth wrong.I used 3 equations with it's condition and made it in for isntruction,while,do-while,if-else,with switch.After i introduce variable a it show me an error "exited with non-zero status".
#include <stdio.h>
#include <math.h>
int main() {
float a,x,b;
float L;
int m;
printf("Enter variables a,x,b:");
scanf("%f%f%f,&a,&x,&b");
printf("For using for instruction (enter 1)");
printf("For using while instruction (enter 2)");
printf("For using do-while instruction (enter 3)");
printf("For using ef instruction (enter 4)");
scanf("%d,&m");
switch(m){
case 1:
for (x=0;x<1.2;x++)
{
L=2*cos(x-(3.14/6));
}
for (x=0;x>= 1.2 && x<=3.9;x++)
{
L=x*x/(a+cos(powf((x+b),3)));
}
for (x=0;x>3.9;x++)
{
L=fabs(x/2*a)+powf(sin(x+1),2);
}
break;
case 2:
while (x<1.2)
{
L=2*cos(x-(3.14/6));
}
while (x>= 1.2 && x<=3.9)
{
L=x*x/(a+cos(powf((x+b),3)));
}
while (x>3.9)
{
L=fabs(x/2*a)+powf(sin(x+1),2);
}
break;
case 3:
do
{
L=2*cos(x-(3.14/6));
}
while (x<1.2);
do
{
L=x*x/(a+cos(powf((x+b),3)));
}
while (x>= 1.2 && x<=3.9);
do
{
L=fabs(x/2*a)+powf(sin(x+1),2);
}
while (x>3.9);
break;
case 4:
if (x<1.2)
{
L=2*cos(x-(3.14/6));
}
else
{
printf("First statement is false");
}
if(x>= 1.2 && x<=3.9)
{
L=x*x/(a+cos(powf((x+b),3)));
}
else
{
printf("Second statement is false");
}
if(x>3.9)
{
L=fabs(x/2*a)+powf(sin(x+1),2);
}
else
{
printf("Third statement is false");
}
break;
default:
printf("\nNo right choices\n");
}
printf("Your answer is: L = %.3f,L");
}

Your problem is that your scanf arguments are not formatted correctly.
Instead of scanf("%f%f%f,&a,&x,&b"); use scanf("%f%f%f",&a,&x,&b);. Same in the second scanf.
The variables addresses are parameters, not part of the string.
When you call it, scanf finds the first %f but it doesn't have any address to put the value into. Or more accuratly, it finds the value it needs from garbage (read about the stack and dynamic number of arguments), because you didn't insert it.

scanf("%f%f%f,&a,&x,&b"); should be like this scanf("%f%f%f",&a,&x,&b);. Please correct where ever you used scanf. Your code is not taking input from user due to wrong syntax. I have compiled and tried it is runnig correctly. Please change scanf syntax every where.
scanf("%f%f%f,&a,&x,&b") to scanf("%f%f%f",&a,&x,&b)
scanf("%d,&m"); to scanf("%d",&m);
printf("Your answer is: L = %.3f,L"); to printf("Your answer is: L = %.3f",L);

You are missing the return 0; Statement at the end of the main function. Since c main function is int main ()

Related

C Loop until the condition is met problem

I want to write a loop that runs until the user enters a number greater than 10, but I have to do something wrong because it creates an infinite loop.
int main()
{
int a;
printf("Enter 'a' value (min 10): ");
scanf("%d",&a);
for(int i=0;a<10;i++){
printf("Enter value>10");
i++;
printf("%d",&a);
}
printf("Result:%d",a+a-2+a-4+a-6+a-8+a-10);
return 0;
}
You mix an index that does not make sense. Also you print the memory address of variable instead of its value, not sure it is what you wanted?
Code partially corrected (because I don't know what is your ultimate goal):
#include <stdio.h>
int main()
{
int a;
do {
printf("Enter 'a' value (min 10): ");
scanf("%d",&a);
printf("\na: %d\n",a);
} while (a <= 10);
printf("Result:%d\n",a+a-2+a-4+a-6+a-8+a-10);
return 0;
}
ps: \n is line return and added do while which is what you want when you want to execute a loop at least once.
Have a look at your for-loop: you let i start at zero, you continue until a is not smaller than ten anymore, but it's not the value of a you need to check, it's the one of i.
In top of that, you are doing a i++ within your for-loop, while this is already covered in the definition of the for-loop.
I think this is the code that you are looking for: See comments
#include <stdio.h>
int main()
{
int a, ok = 0, end_of_input = 0;
do {
printf("Please input an integer value (min. 10): ");
fflush(stdout); // So the user can see the above line!
switch(scanf("%d",&a)) {
case EOF: // End of input - Give up!
end_of_input = 1;
break;
case 1: // Got a number - Check it!
if (a < 10)
{
ok = 1;
} else {
printf("%d - Not appropriate input. Please try again.\n\n",a);
}
break;
default: // Summat else - "eat" the input to the next line
scanf("%*[^\n]\n"); // "eats" the rest of the line in the buffer w/o assignment
break;
}
} while (end_of_input == 0 || ok == 0);
if (ok) { // User entered a valid number
printf("Got a that is smaller than ten %d\n", d);
} else { // We have ran out of input
printf("See you want to leave us :-(\n");
}
return 0;
}
I am not sure what you are trying to achieve but one problem that I found in your logic is you prompting user for input outside the loop. So whenever you enter number less than 10 it always goes in infinite iteration.
Try following code, with scanf inside loop
int main()
{
int a;
printf("Enter 'a' value (min 10): ");
scanf("%d",&a);
int i=0;
for(;a<10;){
printf("Enter value>10");
scanf("%d",&a);
printf("%d",a);
i++;
}
printf("Result:%d",a+a-2+a-4+a-6+a-8+a-10);
return 0;
}

Trying to get a REPEAT UNTIL (DO...WHILE) loop working in C

So I am writing this code for simple shape determining program, and I was trying to loop it, the condition, for the loop to end is when the value of the variable x is equal to 0 (all of teh variables are angels), since an angle cannot be 0, I think it is only logical to have the loop loop until the value entered is 0. However, the code does not seem to stop iterating even after the condition is met. the code is below:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x=0;
int y=0;
int A=0;
do {
printf("What is the value of x?");
scanf("%d", &x);
printf("What is the value of y?");
scanf("%d", &y);
printf("What is the value of A?");
scanf("%d", &A);
if ((A==90)&&(x==y))
{
printf("Square\n");
}
else if ((A==90)&&(x!=y))
{
printf("Rectangle\n");
}
else if ((A==60)||(A==120))
{
printf("Hexagonal\n");
}
else if ((A!=60)||(A!=120)&&(x==y))
{
printf("Rhombic\n");
}
else if ((A!=60)||(A!=120)&&(x!=y))
{
printf("Parallelogram\n");
}
else
{
printf("The values you have entered are not supported by the program,
try again!");
}
}while(x!=0);
printf("Thanks for using the program!");
system("pause");
return 0;
}
I cannot really see what the problem with the condition for the while loop is, please help!
The easiest way may be:
scanf("%d", &x);
if (!x) break; // break the loop, not asking anything else

C program basic calculator

#include<stdio.h>
int main()
{
int a,b;
char op;
scanf("%d",&a);
scanf("%c",&op);
scanf("%d",&b);
int w,x;
w=a+b;
x=a-b;
switch(op)
{
case'+':
{
printf("%d",w);
break;
}
case'-':
{
printf("%d",x);
break;
}
default:
{
printf("Invalid");
break;
}
}
return 0;
}
Every time I enter the second input – the character(+ or -) – it directly goes to invalid in the switch case. What am I doing wrong here?
Your scanf sequence isn't doing what you think it is. Print out the value of op before the switch and you'll see the problem.
Your program works just fine. Your problem is with the input: use 123+456 instead of pressing enter between the decimal and the operator.
Here is an example of your program:
$ ./basic
456-123
333$
I suggest adding \n to each printf() format string to eliminate the prompt displaying in the same line.

Returning integer array values from a function in c

I am really struggling on an assignment i have. I have searched the internet and youtube but i am still none the wiser.
The program will have 5 functions in total, but i am stuck on the first. The program should use a 1-D array to read a 4 digit code(must be 4 single digit numbers) entered by the user. My problem arises when i am trying to return that code from the function. All i am getting is the first number. I am aware that you cannot return an array from a function in c and that you have to use pass by reference, this is where i have a problem i do not completely understand how to do this. my code is below along with the output i recieve.
Any help you can give me would be much appreciated, as ive said before i am really struggling.
//program to enter a code and return the code to main
#include <stdio.h>
#include <stdlib.h>
#define CODE 4
//function prototypes
int enter_code(int* code_arr);
main()
{
int code =0;
int option;
int exit1=0;
do
{
//print the menu on screen
printf("\t \t \t1 - Enter the access code\n");
printf("\t \t \t2 - Encrypt code and verify\n");
printf("\t \t \t3 - Exit the program \n");
scanf("%d",& option);
switch(option)
{
case 1:
{
//call enter_code function
code= enter_code(&code);
printf("\n The returned code is %d \n",code);
break;
}
case 2:
{
break;
}
case 3:
{
// prompt user to a key to exit
printf("\n You choose to exit the program.\n Press a key to exit\n ");
getchar();
exit(0);
break;
}
default:
{
printf("You must enter a number between 1-5\n");
}
}
}//end do()
while(exit1!=5 & exit1 <6);
}//end main
int enter_code (int* code_arr)
{
int password[CODE];
int i;
printf("Enter your 4 digit code \n");
for(i=0;i<CODE;i++)
{
scanf("%d",&password[i]);
}
printf("The code entered is:");
for(i=0;i<CODE;i++)
{
printf("%d",password[i]);
}
return(*password); //how do i return the full array
}
Your function can return the code through the array passed as an argument, and use the function return value to indicate an error. You can pass that to another function too. Your simplified code:
#include <stdio.h>
#include <stdlib.h>
#define CODE 4
int enter_code (int* code_arr)
{
int i;
printf("Enter your 4 digit code\n");
for(i=0;i<CODE;i++)
if (scanf("%d", &code_arr[i]) != 1)
return 0;
return 1;
}
int check_code (int* pass_code, int* user_code)
{
int i;
for(i=0;i<CODE;i++)
if (pass_code[i] != user_code[i])
return 0;
return 1;
}
int main(void)
{
int password[CODE] = {0}, passOK[CODE] = {42,24,0,12345678};
if (!enter_code(password))
printf ("Bad password entry\n");
else {
if (check_code(passOK, password))
printf("You unlocked the vault\n");
else
printf("You don't know the passcode\n");
}
return 0;
}
Program output:
Enter your 4 digit code
42
24
0
12345678
You unlocked the vault

Using getNum(); Properly and Infinite Loop Problems

Just having a few kinks in this assignment I'm trying to do. Basically I need to have a menu, 4 options, two of them accept input from user as the form of a base number and an exponent. The third one outputs the answer of the base raise to the power and then the fourth just exits the program.
I'm having trouble obtaining the users input via getNum(); I'm not too sure how to use it properly. Just looking on some tips on how to make my code work a little better.
Looking for Help:
Accepting user input from two different functions and using it to
output an answer
Working out the infinite loop problem when selecting menu option
Loop back program to main menu after each function is done and only
exit program when menu option 4 is selected
int main(void)
{
int option = 0;
do
{
loadMenu();
while (option<1 || option>4)
{
printf("\nChoose an option between 1 and 4:");
option = getNum();
while (getNum() != '\n');
}
switch (option)
{
case 1:
baseChange(); //Gets base number
break;
case 2:
powerChange(); //Gets exponent
break;
case 3:
calcMath(); //Calculates the answer
break;
default:
break;
}
}
while (option != 4);
printf("Goodbye!\n");
}
void loadMenu() //Menu choices
{
printf("Power Menu:\n" );
printf(" 1. Change base\n");
printf(" 2. Change exponent\n");
printf(" 3. Calculate\n");
printf(" 4. Exit\n");
printf("Option?\n");
}
int baseChange(int base)
{
printf("What is your base?: ");
base = getNum();
while (getNum() != '\n');
return base;
}
int powerChange(int power)
{
printf("What is the power?: ");
power = getNum();
while (getNum() != '\n');
return power;
}
int calcMath(int base, int power)
{
int index = 0;
long answer = 1.00;
for(index = 1; index <= power; index++) answer = answer * base;
{
printf("%d raised to the power of %d is %ld.\n\n", base, power, answer);
}
return answer;
}
I'm having trouble obtaining the users input via getNum(); I'm not too
sure how to use it properly.
You haven't told us anything about this function; it's not part of the C standard.
Just looking on some tips on how to make my code work a little better. Looking for Help:
I think it's a little early for that. Put more effort into solving your problems, and then come back if you have specific questions. More like this one:
Working out the infinite loop problem when selecting menu option
Look at what your program does with option the second time through the loop.
Please Declare the getnum() function before main() like below;
/* declare getnum() prior to its first use */
float getnum(void)
{
float x;
printf("Enter a number: ");
scanf("%f", &x);
return x;
}

Resources