so I declared a float named price and when I try to compile it I get a waring that it would be a double, can someone tell me why C think that this is a double ?
B3N2.c:37: warning: format specifies type 'float *' but the argument has type 'double' [-Wformat]
scanf("%f", la[i].preis);
int main(int argc, char *argv[]) {
struct Lager {
char artikel[200];
int anzahl;
float preis;
} la[200];
printf("Wilkommen bei Lagerverwaltung 97\n\n\nWas möchten sie tun ?\n");
int exit = 0;
int x,v;
int f = 1;
int i = 0;
char ques;
int end;
do {
printf("\n(1)Artikel hinzufügen\n(2)Artikel entnehmen.\n(3)Eintrag suchen.\n(4)Lager ausgeben.\n(5)Exit\n");
scanf("%x",&x);
switch (x) {
case 1://add
do {
printf("\nGebe den namen des Produkts an: ");
scanf("%s", la[i].artikel);
printf("\nAnzahl der verfügbaren Produkte: ");
scanf("%i", &la[i].anzahl);
printf("\ngib den preis des artikels an: ");
scanf("%f", la[i].preis);
printf("\n\nWeiteres Produkt hinzufügen ? (J/N)");
scanf("%s", &ques);
switch (ques) {
case 'J':
v++;
f++;
break;
case 'N':
end = 1;
v = 0;
break;
default:
printf("Falsche Eingabe\n");
break;
}
} while (end != 1);
if (v >= 2) {
printf("Produkt wurde Erfolgreich hinzugefügt\n\n");
}else {
printf("Produkte wurden Erfolgreich hinzugefügt\n\n");
}
break;
sorry for the dumb question but I tryed to fix it and now i'm totally Overwhelmed
scanf() needs a pointer to the space (that is, the variable) where the scanned value will be stored. But your call hands over the value of la[i].preis which is a float that is cast automatically into a double.
Change your code to give the address of la[i].preis to scanf() by using the & operator like this:
scanf("%f", &la[i].preis);
Related
Hello so I've been working a little prgramme which is sort of a calculator (I'm a beginner) and well as you can see in the tittle at then end of the code, the two if strcmp doesn't work. And vscode is telling me (for the strcmp) Exception has occurred. Segmentation fault. But gcc is telling me what is in the tittle.
#include <stdio.h>
#include <string.h>
int main()
{
float num1;
float num2;
float anwser;
int rnum = 1;
int hi = 0;
char operator;
char ifyorn;
char y = 'y';
char n = 'n';
while (hi == 0)
{
printf("Enter operator +, -, /, x: ");
scanf(" %c", &operator);
printf("Enter num %d :", rnum++);
scanf("%f", &num1);
printf("Enter num %d :", rnum++);
scanf("%f", &num2);
switch (operator)
{
case '+':
anwser = num1 + num2;
printf("Do you want to continue y/n \n");
scanf(" %c", &ifyorn);
break;
case '-':
anwser = num1 - num2;
printf("Do you want to continue y/n \n");
scanf(" %c", &ifyorn);
break;
case 'x':
anwser = num1 * num2;
printf("Do you want to continue y/n \n");
scanf(" %c", &ifyorn);
break;
case '/':
anwser = num1 / num2;
printf("Do you want to continue y/n \n");
scanf(" %c", &ifyorn);
break;
default:
printf("This is not a valid character please try again :(");
break;
}
if(strcmp (ifyorn, n) == 0)
{
printf("%f", anwser);
hi == 1;
}
if(strcmp (ifyorn, y) == 0)
{
hi == 0;
}
}
}
The variables ifyorn, y and n are declared having the type char.
char ifyorn;
char y = 'y';
char n = 'n';
The function strcmp expects arguments of the pointer type char * that point to strings.
So these if statements
if(strcmp (ifyorn, n) == 0)
and
if(strcmp (ifyorn, y) == 0)
are incorrect. Instead you should write
if ( ifyorn == n )
and
if ( ifyorn == y )
Also instead of assignments you are using the comparison operator in these statements
hi == 1;
and
hi == 0;
You need to write
hi = 1;
and
hi = 0;
Increasing the variable rnum looks senseless
printf("Enter num %d :", rnum++);
scanf("%f", &num1);
printf("Enter num %d :", rnum++);
scanf("%f", &num2);
Why not just to write
printf("Enter num %d :", 1 );
scanf("%f", &num1);
printf("Enter num %d :", 2 );
scanf("%f", &num2);
And in the code snippet under the label default you should add one more statement
default:
printf("This is not a valid character please try again :(");
ifyorn = y;
break;
You don't have to be mean to the guy ,he is learning.
You are getting this error because you are passing characters to strcmp() instead of pointers to characters.
Here is more information regarding that function.
https://www.programiz.com/c-programming/library-function/string.h/strcmp
I try to use Y and N in a switch statement but when I try to compile it I got an error what say " error: statement requires expression of integer type ('char [0]' invalid)
switch (xx) {
"
i also got a warning " warning: incompatible pointer to integer conversion passing 'char [1]' to parameter of type 'char' [-Wint-conversion]
sucheZeichen(name, imya); "
int main() {
char name[200];
printf("Please tell me your name:");
fflush(stdin);
scanf("%s", name);
printf("%s has %i letters\n", name, langeZeichne(name));
// frage
int end = 0;
char xx[0];
char imya[1];
do {
fflush(stdin);
printf("would you like to count a letter in %s (Y / N)?\n", name);
scanf("%c", xx);
switch (xx) {
case "Y":
printf("Please enter a letter\n");
scanf("%s", imya);
sucheZeichen(name, imya);
break;
case "N":
printf("Have a nice Day!");
end = 0;
break;
default:
printf("Wrong input\n");
break;
}
}while (end==0);
}
First of all, if you want to read a letter into xx don't use an array, and definitly not an array of size 0.
Second, your switch receive an char[] instead of a char.
Next, in the cases use chars instead of strings.
Moreover, according to the information in the question, sucheZeichen gets a char as a second parameter (or an int) so the same problem with xx as an array apply for imya as well.
Also, I added \n to the scanf so it won't read the newline character.
Your code should like like:
int main() {
char name[200];
printf("Please tell me your name:");
fflush(stdin);
scanf("%s", name);
printf("%s has %i letters\n", name, langeZeichne(name));
// frage
int end = 0;
char xx;
char imya;
do {
fflush(stdin);
printf("would you like to count a letter in %s (Y / N)?\n", name);
scanf("%c\n", &xx);
switch (xx) {
case 'Y':
printf("Please enter a letter\n");
scanf("%c\n", &imya);
sucheZeichen(name, imya);
break;
case 'N':
printf("Have a nice Day!");
end = 0;
break;
default:
printf("Wrong input\n");
break;
}
}while (end==0);
}
I try to give my Void function a struct but when I do this I get a errors.
first:error: subscript of pointer to incomplete type 'struct Lager'.
and a warning:warning: incompatible pointer types passing 'Lager (*)[200]' to parameter of type 'struct Lager *' [-Wincompatible-pointer-types].
and
warning: declaration of 'struct Lager' will not be visible outside of this function [-Wvisibility].
I already asked my fellow students but no-one of them could help me.
void case1(struct Lager *wh, int l){
int end;
int v;
char ques;
do {
printf("\nEnter the product name: ");
scanf("%s", *(wh[l]).artikel);
printf("\nAmount of products: ");
scanf("%i", &*(wh[l]).anzahl);
printf("\n\nAdd another product ? (Y/N)");
// add a space before % to skip leading whitespace
scanf(" %c", &ques);
switch (ques) {
case 'Y':
v++;
l++;
break;
case 'N':
end = 1;
v = 0;
l++;
break;
default:
printf("Wrong entry\n");
end = 1;
break;
}
} while (end != 1);
if (v >= 2) {
printf("Product added successfully\n\n");
}else {
printf("Products have been successfully added\n\n");
}
}
int main() {
typedef int item;
typedef char max;
typedef int GanzvieleBuchstaben;
//unien wär die bessere variante (eigentlich). weil platzsparender
typedef struct managementtool
{
max artikel[200];
item anzahl;
}Lager;
Lager lager[200];
printf("Welcome to Warehouse Management 97\n\n\nWhat do you want to do ?\n");
GanzvieleBuchstaben a,l,f,i,v,x,p,c,я,и,ii;
int exit, end, all;
int b = 201;
char ques, nu1;
char find [100];
do {
printf("\n(1)Add article\n(2)Edit article.\n(3)Search entry.\n(4)Show stock.\n(5)Exit\n");
scanf("%i",&x);
switch (x) {
case 1://add
case1(&lager, l);
*Irrelevant stuff down there*
First of all, im a begginer C programmer, studying in a university. I need to do a program that is kinda like a data base. Thus far i have made the function to input new data, it works, but compiler shows a few warning and notes, i tried a lot of things, but nothing helped. Here is the code:
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
struct subjects{
char *subjName[100];
char *lectName[100];
char *lectSurname[100];
double credits;
double studentnum;
}subj;
char subjectname[][100];
char lecturername[][100];
char lecturersurname[][100];
int credits[100];
int students[100];
int newentry=0;
void listInput();
void listEdit();
void listDelete();
void listPrint();
int userChoice();
int main() {
int select;
int r=1;
while(r!=0){
select=userChoice();
switch(select){
case 1:
listPrint();
break;
case 2:
listInput();
break;
case 3:
listDelete();
break;
case 4:
listEdit();
break;
case 0:
r=0;
break;
}
}
return 0;
}
int userChoice(){
int choice,input=0,check;
printf("(1). View all the data\n");
printf("(2). Enter new data\n");
printf("(3). Delete data\n");
printf("(4). Edit data\n");
printf("(0). Exit\n");
printf("-----------------------------\n");
while(input!=1){
printf("Enter your choice\n");
scanf("%d", &choice);
if(choice>4 || choice<0){
printf("Invalid input \n");
}
else
input = 1;
}
return choice;
}
void listPrint(){
for(int i=0; i<newentry; i++){
printf("%d, %s, %s, %s, %d, %d\n",i+1, subjectname[i], lecturername[i], lecturersurname[i], credits[i], students[i]);
}
}
void listInput(){
int i;
char firstLetter,term;
printf("Enter the name of the subject \n");
while(scanf("%s", &subj.subjName)!=1)
printf("Error, subject name must be a text ");
printf("Enter the name of the lecturer \n");
int valid=0;
while(valid!=1){
while(scanf("%s", &subj.lectName)!=1)
printf("Error, lecturer's' name must be a text ");
firstLetter=*subj.lectName;
if(firstLetter>65 && firstLetter<90 && isalpha(firstLetter)){
valid=1;
}
else
printf("Error, lecturer's name must start with a capital letter, try again \nEnter the name of the lecturer \n");
}
valid=0;
printf("Enter the surname of the lecturer\n");
while(valid!=1){
while(scanf("%s", &subj.lectSurname)!=1)
printf("Error, lecturer's surname must be a text ");
firstLetter=*subj.lectSurname;
if(firstLetter>65 && firstLetter<90 && isalpha(firstLetter)){
valid=1;
}
else
printf("Lecturer's surname must start with a capital letter, try again \nEnter the surname of the lecturer \n");
}
printf("Enter the amount of credits in course \n");
while(1){
scanf("%lf", &subj.credits);
if(subj.credits<0 || subj.credits != (int)subj.credits)
printf("Error, amount of credits must be a positive integer, try again \n");
if(subj.credits>0 && subj.credits == (int)subj.credits)
break;
}
printf("Enter the number of students \n");
while(1){
scanf("%lf", &subj.studentnum);
if(subj.studentnum<0 || subj.studentnum != (int)subj.studentnum)
printf("Error, number of students must be a positive integer,try again \n");
if(subj.studentnum>0 && subj.studentnum == (int)subj.studentnum)
break;
}
printf("Added a new entry.\n\n");
strncpy(subjectname[newentry], subj.subjName, 99);
strncpy(lecturername[newentry], subj.lectName, 99);
strncpy(lecturersurname[newentry], subj.lectSurname, 99);
credits[newentry]=subj.credits;
students[newentry]=subj.studentnum;
newentry++;
}
void listDelete(){
printf("33333333333");
}
void listEdit(){
printf("4444444");
}
And the warnings:
In function 'listInput':
96 14 [Warning] assignment makes integer from pointer without a cast
108 14 [Warning] assignment makes integer from pointer without a cast
132 33 [Warning] passing argument 2 of 'strncpy' from incompatible pointer type
79 9 [Note] expected 'const char * restrict' but argument is of type 'char **'
133 34 [Warning] passing argument 2 of 'strncpy' from incompatible pointer type
79 9 [Note] expected 'const char * restrict' but argument is of type 'char **'
134 37 [Warning] passing argument 2 of 'strncpy' from incompatible pointer type
79 9 [Note] expected 'const char * restrict' but argument is of type 'char **
At top level:
14 6 [Warning] array 'subjectname' assumed to have one element
15 6 [Warning] array 'lecturername' assumed to have one element
16 6 [Warning] array 'lecturersurname' assumed to have one element
What can i do to fix those warnings? The program works just fine, but i cant pass if i dont fix the warnings.
Here is a better way to do things
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
char *subjName;
char *lectName;
char *lectSurname;
double credits;
double num_students;
}Subject;
typedef struct{
Subject **subjs;
int num_subjs;
}Subjects;
void listInput();
void listEdit();
void listDelete();
void listPrint();
int userChoice();
int main() {
Subjects *subjects = malloc(sizeof(Subjects));
subjects->num_subjs = 0;
subjects->subjs = NULL;
int r=1;
while(r!=0){
int select=userChoice();
switch(select){
case 1:
listPrint(subjects);
break;
case 2:
listInput(&subjects);
break;
case 3:
listDelete();
break;
case 4:
listEdit();
break;
case 0:
r=0;
break;
}
}
return 0;
}
int userChoice(){
int choice,input=0,check;
printf("(1). View all the data\n");
printf("(2). Enter new data\n");
printf("(3). Delete data\n");
printf("(4). Edit data\n");
printf("(0). Exit\n");
printf("-----------------------------\n");
while(input!=1){
printf("Enter your choice\n");
scanf("%d", &choice);
if(choice>4 || choice<0){
printf("Invalid input \n");
}
else
input = 1;
}
return choice;
}
void listPrint(Subjects *subjects){
for(int i=0; i< subjects->num_subjs; i++){
printf("%d, %s, %s, %s, %d, %d\n",i+1, subjects->subjs[i]->subjName, subjects->subjs[i]->lectName, subjects->subjs[i]->lectSurname, subjects->subjs[i]->credits, subjects->subjs[i]->num_students);
}
}
char *getln()
{
char *line = NULL, *tmp = NULL;
size_t size = 0, index = 0;
int ch = 1;
while (ch) {
ch = getc(stdin);
if (ch == '\n')
ch = 0;
if (size <= index) {
size += 1;
tmp = realloc(line, size);
if (!tmp) {
free(line);
line = NULL;
break;
}
line = tmp;
}
line[index++] = ch;
}
return line;
}
int isText(char *str,char *name){
for(int i = 0; i < strlen(str);i++){
if((str[i]<'A' || str[i]>'z') && str[i]!=' '){
printf("Error, %s must be a text \n",name);
return 0;
}
}
return 1;
}
int isNegative(int n){
if(n < 0){
printf("Error, number of students must be a positive integer,try again \n");
return 1;
}
return 0;
}
void listInput(Subjects **p_subjects){
while ( getchar() != '\n' );
Subject *new_subj = malloc(sizeof(Subject));
new_subj->subjName = NULL;
new_subj->lectName = NULL;
new_subj->lectSurname = NULL;
new_subj->credits = 0;
new_subj->num_students = 0;
do{
printf("Enter the name of the subject \n");
new_subj->subjName = getln();
}while(!isText(new_subj->subjName,"Subject name"));
do{
printf("Enter the name of the lecturer \n");
new_subj->lectName = getln();
}while(!isText(new_subj->lectName,"Lecturer's name"));
do{
printf("Enter the surname of the lecturer\n");
new_subj->lectSurname = getln();
//Convert to uppercase
new_subj->lectSurname[0] &= '_';
}while(!isText(new_subj->lectSurname,"Lecturer's surname"));
do{
printf("Enter the number of credits \n");
scanf("%d", &new_subj->credits);
}while(isNegative(new_subj->num_students));
do{
printf("Enter the number of students \n");
scanf("%d", &new_subj->num_students);
}while(isNegative(new_subj->num_students));
(*p_subjects)->subjs = realloc((*p_subjects)->subjs,sizeof(Subject*)*(++(*p_subjects)->num_subjs));
(*p_subjects)->subjs[(*p_subjects)->num_subjs-1] = new_subj;
printf("Added a new entry.\n\n");
}
void listDelete(){
printf("33333333333");
}
void listEdit(){
printf("4444444");
}
I am pretty new in C and I came across this problem and didn't understand why does this happen. After I run the following code there's an error message which says:
"Run-Time Check Failure #2 - Stack around the variable 'op' was corrupted".
FYI, the program should ask the user for 2 integers and a mathtematical operator, and then it performs the operation on the two numbers (e.g 5,3,+ --> 5+3=8). It works just fine, except the error message that pops in the end.
I'm only interested in understanding why this message pops, there is no need to give me notes about my coding style etc. - it is all still new to me...
Thanks
#include <stdio.h>
#include <stdlib.h>
void askUser();
double calculateImpl(int *, char *);
typedef struct _myStruct
{
double result;
double(*calculate) (int *, char *);
} myStruct;
int main(void) {
puts("Hello World!");
askUser();
return EXIT_SUCCESS;
}
void askUser(){
int numbers[2] = { 0, 0 };
char op[1] = { '.' };
printf("Please choose 2 integers:\n");
printf("1st integer: ");
scanf("%d", &numbers[0]);
printf("\n2nd integer: ");
scanf("%d", &numbers[1]);
printf("\nchoose a mathematical operator\n(+,-,*,/; can't choose / if the second number is 0: ");
scanf("%s", &op[0]);
while (numbers[1] == 0 && op[0] == '/'){
printf("\ncan't choose / while the second number is 0.\nchoose a different operation: ");
scanf("%s", &op[0]);
}
printf("the result of %d %s %d is ", numbers[0], &op[0], numbers[1]);
myStruct strct;
strct.calculate = calculateImpl;
strct.result = strct.calculate(numbers, op);
printf("%lf\n", strct.result);
}
double calculateImpl(int * numbers, char * op){
double result = 0;
switch (op[0])
{
case '+':
result = numbers[0] + numbers[1];
break;
case '-':
result = numbers[0] - numbers[1];
break;
case '*':
result = numbers[0] * numbers[1];
break;
case '/':
result = numbers[0] / numbers[1];
break;
default:
break;
}
return result;
}
scanf("%s", &op[0]);
^ use %c
Use %c specifier to take input in character variable . You pass wring argument to %s specifier.
Also there is scanf in while loop , use correct specifier there also .
Declare op as char -
char op; // you don't need char op[1]
Wrtie like this -
scanf(" %c", &op); // at both places
And inside after loop this line -
printf("the result of %d %s %d is ", numbers[0], &op[0], numbers[1]);
To print char don't pass its address and use %c -
printf("the result of %d %c %d is ", numbers[0], op, numbers[1]);
Also then pass your function char not char * -
double calculateImpl(int *, char );
and make changes accordingly in your program.