This question already has an answer here:
C - Read special characters like 'ã' using scanf
(1 answer)
Closed 7 months ago.
I'm building a system and i need a char inside a struct to receive a name and 2 values, but when I see the result, the name is always broken from a special character.
#define NOMINMAX
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
float preco;
int quant;
char* prod[50];
} info;
void imprime(info j)
{
printf("|| Produto: %s || preço: %.2f || quantidade: %d ||\n", j.prod, j.preco, j.quant);
}
int main()
{
SetConsoleCP(CP_UTF8);
SetConsoleOutputCP(CP_UTF8);
system("color 0F");
info lojas[50];
for (int x = 0; x < 1; x++)
{
printf("Digite o nome do produto: ");
scanf(" %[^\n]s", &lojas[x].prod);
printf("\nDigite o preço do produto: ");
scanf(" %f", &lojas[x].preco);
printf("\nDigite a quantidade desejada do produto: ");
scanf(" %d", &lojas[x].quant);
system("cls");
}
for (int x = 0; x < 1; x++)
{
imprime(lojas[x]);
}
}
imput:
Digite o nome do produto: Açucar
Digite o preço do produto: 1
Digite a quantidade desejada do produto: 1
expected output:
|| Produto: Açucar || preço: 1.00 || quantidade: 1 ||
result received
|| Produto: A || preço: 1.00 || quantidade: 1 ||
I think the problem may be in these lines:
SetConsoleCP(CP_UTF8);
SetConsoleOutputCP(CP_UTF8);
without them the code works, but the special characters inside printf stop working.
I already tried to use the <locale.h> library but for reasons I don't know it doesn't work in my compiler
I use the embarcadeiro dev-c++
Hi, that's because in C, you should scan a string with another function like gets().
For that IDK if you have to add the library <strings.h> but an example is:
int main () {
char str[50];
printf("Inster a string: ");
gets(str);
printf("The string is: %s",str);
return(0);
}
I recommend you to add the library <string.h> because its easier to make more functions.
See you :)
Related
Basically, I have to make a currency prices conversion with a menu (Im in first year) but the second scanf that executes when you select the 0) is totally skipped; after that the program prints the menu again around 5 times and then it lets you select an option again. This is the only part that is not working, the rest is fine.
You may think that the problem is that the buffer has a newline stored from the previous scanf, but that is not the case as I am making sure to clean it. Also, don't mind the strings, they are just in spanish.
Thanks for your time!
'''
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(void) {
char e = ' ';
float le = 1.1, de = 0.9, din = 1, c1, c2;
while (e != 's') {
puts("\n ================================================================");
puts(" CONVERSION DE DIVISAS");
puts(" ================================================================");
puts("0) Introducir valores de cotizacion de monedas");
puts("a) Convertir euros a dolares y libras");
puts("b) Convertir dolares a euros y libras");
puts("c) Convertir libras a euros y dolares");
puts("s) Salir");
printf("\nIntroduzca su eleccion: ");
scanf(" %c", &e);
e = tolower(e);
fflush(stdin);
switch (e) {
case '0':
printf("\nIntroduzca cuantas libras son un euro: ");
scanf(" %9.4f ", &le);
fflush(stdin);
printf("Introduzca cuantos dolares son un euro: ");
scanf(" %9.4f ", &de);
fflush(stdin);
break;
default:
printf("\nOpcion incorrecta");
break;
}
}
return 0;
}
'''
I am System Analyst & Developer student, I'm trying to write code but I'm stuck, structs and fuctions are easy to understand but working with both together... is a... crap...
What an I doing wrong?
#include<stdio.h>
#include<stdlib.h>
#include<locale.h>
typedef struct mercearia
{
char mercadoria[20];
int quantidade;
int valor;
} mercearia;
void EstruturaCadastro(struct mercearia *m)
{
int i;
for(i = 0; i < 5; i++)
{
printf("\n");
setbuf(NULL, stdin);
printf("Insira nome do produto a cadastrar: ");
scanf("%s", &(*m)[i].mercadoria);
setbuf(NULL, stdin);
printf("Insira a quantidade do produto: ");
scanf("%i", &(*m)[i].quantidade);
setbuf(NULL, stdin);
printf("Insira o valor do produto: R$ ");
scanf("%i", &(*m)[i].valor);
}
}
int main(void)
{
setlocale(LC_ALL, "");
struct mercearia m[5];
EstruturaCadastro(&m);
}
It gives me huge error list. Then I wrote same code but without creating array of struct and the code worked perfectly. I am missing up something.
#include<stdio.h>
#include<stdlib.h>
#include<locale.h>
typedef struct mercearia
{
char mercadoria[20];
int quantidade;
int valor;
} mercearia;
void EstruturaCadastro(struct mercearia *x)
{
printf("\n");
printf("Insira nome do produto: ");
scanf("%s", &(*x).mercadoria);setbuf(NULL, stdin);
printf("Insira a quantidade do produto: ");
scanf("%i", &(*x).quantidade);setbuf(NULL, stdin);
printf("Insira o valor do produto: R$ ");
scanf("%i", &(*x).valor);setbuf(NULL, stdin);
}
int main(void)
{
setlocale(LC_ALL, "");
struct mercearia produto;
EstruturaCadastro(&produto);
}
You need to understand the 2nd example (simple) before trying the 1st (harder)
void EstruturaCadastro(struct mercearia *x)
{
printf("\n");
printf("Insira nome do produto: ");
scanf("%s", x->mercadoria);
printf("Insira a quantidade do produto: ");
scanf("%i", &x->quantidade);
printf("Insira o valor do produto: R$ ");
scanf("%i", &x->valor);
}
No need for the setbuf() call, you flush stdin when you press the key
Once you've done this, you can change to m[i]->mercadoria, etc in your first example.
There are all kinds of wrong with your original function. You're passing it a pointer to struct mercearia which is fine for passing in your array. Inside the function you can treat m the same way you would an array, so for example m[0].valor is how you'd access the valor value for the first element.
This means that your various calls to scanf become
scanf("%s", m[i].mercadoria);
scanf("%i", &m[i].quantidade);
scanf("%i", &m[i].valor);
Note, that for reading in a string unlike other data types, you don't need to pass in the pointer to the string so you don't need the preceeding &.
This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 3 years ago.
I am programming a simulation, and when the user chooses to create a new tag, the user is supposed to enter a tag ID, the tag's owner, and the object the tag represents. What the program is doing is just skipping over command that scans for the owner, and I'm not quite sure why. My codes are below (the functions are in iotlib.cpp):
iotlib.cpp
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX 20
struct tagInfo
{
char owner[MAX];
char object[MAX];
int id;
};
struct tre //TRE = Tag Read Event
{
int id;
char node[MAX];
int dx;
};
void initTag(struct tagInfo tag[], int numTags)
{
for(int i=0; i<numTags; i++)
{
printf("Enter the tag ID number: ");
scanf("%i", &tag[i].id);
printf("Enter owner of tag: ");
scanf("%c", &tag[i].owner);
printf("Enter the object the tag is attached to: ");
scanf("%c", &tag[i].object);
}
}
void generateTRE(struct tre event[], int numEvents)
{
for(int i=0; i<numEvents; i++)
{
printf("Enter tag ID: ");
scanf("%i", &event[i].id);
printf("Enter node: ");
scanf("%c", &event[i].node);
printf("Enter distance from node as an integer number of feet: ");
scanf("%c", &event[i].dx);
}
}
void triangulationSimulate(struct tre event1, struct tre event2, int numEvents)
{
if(numEvents>1 && event1.id==event2.id)
{
printf("Node %c", event1.node);
for(int i=0; i<event1.dx; i++)
{
printf(" ");
}
printf("Tag %i", event1.id);
for(int i=0; i<event2.dx; i++)
{
printf(" ");
}
printf("Node %c", event2.node);
}
}
void getTagInfo(struct tagInfo tag)
{
printf("The tag with ID %i represents a/an %c belonging to %c", tag.id, tag.object, tag.owner);
}
main.cpp
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "iotlib.cpp"
void execute();
int main(void)
{
execute();
return 0;
}
void execute()
{
struct tagInfo tags[5];
struct tre events[5];
int choice, numTags, numEvents;
printf("!Simulation supports a maximum of 5 tags and 5 nodes!\n\n");
printf("Choose a function by entering it's number:\n1. Create tags\n2. Generate Tag Read Events\n3. Triangulate tag\n4. Recall tag metadata\n\n");
scanf("%i", &choice);
if(choice==1)
{
printf("Enter the number of tags to initialize (max of 5): ");
scanf("%i", &numTags);
if(numTags<1 || numTags>5)
{
printf("Invalid datum.\n");
}
else
{
initTag(tags, numTags);
}
}
else if(choice==2)
{
printf("Enter the number of TRE's to be generated (max of 5: ");
scanf("%i", &numEvents);
if(numEvents<1 || numEvents>5)
{
printf("Invalid datum.\n");
}
else
{
generateTRE(events, numEvents);
}
}
else if(choice==3)
{
int eventX, eventY;
printf("Enter two existing TRE numbers to use, separated by a space: ");
scanf("%i %i", &eventX, &eventY);
triangulationSimulate(events[eventX], events[eventY], numEvents);
}
else if(choice==4)
{
int tagNum;
printf("Enter a tag number: ");
scanf("%i", &tagNum);
getTagInfo(tags[tagNum-1]);
}
else
{
printf("Invalid selection.\n");
}
execute();
}
Point 1 [Programmatic error]
The problem here is the usage with %c format specifier. It counts the previously entered \n, stored by pressing the ENTER key after previous input. What you want is
scanf(" %c", &tag[i].owner);
^
|
note the space
to skip any leading whitespace like character (including \n) before the actual input.
Point 2 [Logical Error]
As per your code here, to scan a string input, you need to use %s format specifier.
So, finally, your code should look like
scanf("%s", tag[i].owner); // if tag[i].owner is char array
or
scanf(" %c", &tag[i].owner); // if tag[i].owner is a char, just in case
%c is the specifier for a character, you're trying to input a string, not a character. See scanf documentation. What you want to use is the %s specifier for a string.
Basically I'm a beginner coder and this is what I wrote:
#include <stdio.h>
#include <stdlib.h>
int main()
{
system("COLOR 0A");
char playerName[13];
char playerGender;
int playerAge;
printf("Please input your name and age!\nName: ");
scanf("%s", playerName);
printf("Age (from 18 to 50): ");
scanf("%d", &playerAge);
label:
if(playerAge > 18 && playerAge < 50)
{
printf("What gender are you, M(male) or F(female): ");
scanf("%c", playerGender);
gender:
if(playerGender == 'M' || playerGender == 'F'){
printf("Okay, so your name is %s, you're %d years old and you're a %s.", playerName, playerAge, playerGender);
}else{
printf("Try again.\n\n"
"What gender are you, M(male) or F(female): ");
scanf("%c", playerGender);
goto gender;
}
}else{
printf("Wrong, try again.\n"
"Age (from 18 to 50): ");
scanf("%d", &playerAge);
goto label;
}
return 0;
}
When I put the required age to continue, it crashes on the scanf for the playerGender. Right after it shows me the question about my gender? Where is my mistake?
try:
scanf("%c", &playerGender);
instead of
scanf("%c", playerGender);
as scanf takes a pointer and not a reference to the variable you are trying to fill.
I am also beginner but I think that you need to write thisscanf("%s", playerName); like thisscanf("%12s", playerName);
BTW let me know if that works.
This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 3 years ago.
I am programming a simulation, and when the user chooses to create a new tag, the user is supposed to enter a tag ID, the tag's owner, and the object the tag represents. What the program is doing is just skipping over command that scans for the owner, and I'm not quite sure why. My codes are below (the functions are in iotlib.cpp):
iotlib.cpp
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX 20
struct tagInfo
{
char owner[MAX];
char object[MAX];
int id;
};
struct tre //TRE = Tag Read Event
{
int id;
char node[MAX];
int dx;
};
void initTag(struct tagInfo tag[], int numTags)
{
for(int i=0; i<numTags; i++)
{
printf("Enter the tag ID number: ");
scanf("%i", &tag[i].id);
printf("Enter owner of tag: ");
scanf("%c", &tag[i].owner);
printf("Enter the object the tag is attached to: ");
scanf("%c", &tag[i].object);
}
}
void generateTRE(struct tre event[], int numEvents)
{
for(int i=0; i<numEvents; i++)
{
printf("Enter tag ID: ");
scanf("%i", &event[i].id);
printf("Enter node: ");
scanf("%c", &event[i].node);
printf("Enter distance from node as an integer number of feet: ");
scanf("%c", &event[i].dx);
}
}
void triangulationSimulate(struct tre event1, struct tre event2, int numEvents)
{
if(numEvents>1 && event1.id==event2.id)
{
printf("Node %c", event1.node);
for(int i=0; i<event1.dx; i++)
{
printf(" ");
}
printf("Tag %i", event1.id);
for(int i=0; i<event2.dx; i++)
{
printf(" ");
}
printf("Node %c", event2.node);
}
}
void getTagInfo(struct tagInfo tag)
{
printf("The tag with ID %i represents a/an %c belonging to %c", tag.id, tag.object, tag.owner);
}
main.cpp
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "iotlib.cpp"
void execute();
int main(void)
{
execute();
return 0;
}
void execute()
{
struct tagInfo tags[5];
struct tre events[5];
int choice, numTags, numEvents;
printf("!Simulation supports a maximum of 5 tags and 5 nodes!\n\n");
printf("Choose a function by entering it's number:\n1. Create tags\n2. Generate Tag Read Events\n3. Triangulate tag\n4. Recall tag metadata\n\n");
scanf("%i", &choice);
if(choice==1)
{
printf("Enter the number of tags to initialize (max of 5): ");
scanf("%i", &numTags);
if(numTags<1 || numTags>5)
{
printf("Invalid datum.\n");
}
else
{
initTag(tags, numTags);
}
}
else if(choice==2)
{
printf("Enter the number of TRE's to be generated (max of 5: ");
scanf("%i", &numEvents);
if(numEvents<1 || numEvents>5)
{
printf("Invalid datum.\n");
}
else
{
generateTRE(events, numEvents);
}
}
else if(choice==3)
{
int eventX, eventY;
printf("Enter two existing TRE numbers to use, separated by a space: ");
scanf("%i %i", &eventX, &eventY);
triangulationSimulate(events[eventX], events[eventY], numEvents);
}
else if(choice==4)
{
int tagNum;
printf("Enter a tag number: ");
scanf("%i", &tagNum);
getTagInfo(tags[tagNum-1]);
}
else
{
printf("Invalid selection.\n");
}
execute();
}
Point 1 [Programmatic error]
The problem here is the usage with %c format specifier. It counts the previously entered \n, stored by pressing the ENTER key after previous input. What you want is
scanf(" %c", &tag[i].owner);
^
|
note the space
to skip any leading whitespace like character (including \n) before the actual input.
Point 2 [Logical Error]
As per your code here, to scan a string input, you need to use %s format specifier.
So, finally, your code should look like
scanf("%s", tag[i].owner); // if tag[i].owner is char array
or
scanf(" %c", &tag[i].owner); // if tag[i].owner is a char, just in case
%c is the specifier for a character, you're trying to input a string, not a character. See scanf documentation. What you want to use is the %s specifier for a string.