C struct glitch? (I am new to programing in C) - c

I am learning how to create struct's and I am stuck on a program I made. Everything works fine until I try to input "2". When the program prints the symbol it's supposed to be "He" but prints "HeHelium" instead. I can't figure out what's wrong and why it's printing he.symbol and he.name all in one line. Link to image below.
#include <stdio.h>
#include <stdlib.h>
struct Element {
int num;
double mass;
char symbol[2];
char name[20];
};
int main()
{
struct Element h;
h.num = 1;
h.mass = 1.008;
strcpy(h.symbol, "H");
strcpy(h.name, "Hydrogen");
struct Element he;
he.num = 2;
he.mass = 4.0026;
strcpy(he.symbol, "He");
strcpy(he.name, "Helium");
int number;
printf("Please enter the number of the element you want info on. \n");
scanf("%d", &number);
if (number == 1 /*&& !(number > 1)*/) {
printf("Atomic Number: %d \n", h.num);
printf("Atomic Mass: %.3f \n", h.mass);
printf("Atomic Symbol: %s \n", h.symbol);
printf("Atomic Name: %s \n", h.name);
} else if (number == 2) {
printf("Atomic Number: %d \n", he.num);
printf("Atomic Mass: %.3f \n", he.mass);
printf("Atomic Symbol: %s \n", he.symbol);
printf("Atomic Name: %s \n", he.name);
} else {
printf("Invalid number! \n");
printf("Or that element hasn't been added to the date base yet. \n");
printf("Try back later \n");
}
return 0;
}
When I input "2":

You have assigned Element.symbol with only 2byte which can only store string with only one character as the other character will be used for null character. You should write "symbol[3]" as the symbol of elements in periodic table are no longer than 2 characters. Also, your code can become quite messy if you want to assign values for all 118 elements. You can write your code like below:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Elements {
int num;
double mass;
char symbol[3];
char name[20];
};
void print(struct Elements element)
{
printf("Atomic Number: %d \n", element.num);
printf("Atomic Mass: %.3f \n", element.mass);
printf("Atomic Symbol: %s \n", element.symbol);
printf("Atomic Name: %s \n", element.name);
}
void assignElement(struct Elements *givenElement, int num, float mass, char symbol[3],
char name[20])
{
givenElement->num=num;
givenElement->mass=mass;
strcpy(givenElement->symbol,symbol);
strcpy(givenElement->name,name);
}
int main()
{
struct Elements element[119];
assignElement(&element[1], 1, 1.008, "H", "Hydrogen");
assignElement(&element[2], 2, 4.06, "He", "Helium");
assignElement(&element[3], 3, 6.09, "Li", "Lithium");
int number;
printf("Please enter the number of the element you want info on. \n");
scanf("%d", &number);
if (number < 119 && number > 0) {
print(element[number]);
}
else {
printf("Invalid number! \n");
printf("Or that element hasn't been added to the date base yet. \n");
printf("Try back later \n");
}
return 0;
}

Related

1.How can I compare 2 chars 2.How can I increase the size of my dynamic array using realloc and print the information it has

I am writing a program where if the user chooses "2" the program will add a new student record and if he chooses "4" then it will compare his surname with all the other surnames, it will find everyone that has the same surname and print their school information. The problems are the following:
When the user selects "2" my program actually shows all the prompts that are needed and you can input all the information however when I try to print it just gives me 0 0 0.000000 .When it should have given me 2(id) B(name) b(surname) 1(semester) 4(grade).
When the user selects "4" it reads the surname but when it goes to compare it with the others to see if it exists or not it just crashes.
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct student{
int id;
char name[50];
char surname[50];
int semester;
float grade;
}student;
int main()
{
int x,std,i;
x=0;
int car=0;
int flag=1;
struct student *ptr = NULL;
while (x!=8)
{
printf("\n1. Initialize student list\n2. Add a student record\n3. Delete a student record\n4. Display a student record by student surname\n5. Display students passed\n6. Display students failed\n7. Display all student records\n8. Exit");
printf("\nYour choice: ");
scanf("%d",&x);
if (x==1)
{
printf("How many new students? ");
scanf("%d",&std);
ptr = (student*)malloc(std*sizeof(student));
for(i = 0; i < std; i++){
printf("Enter detail of student #%d\n", (i + 1));
ptr[i].id=i+1;
printf("Enter first name: ");
scanf("%s", ptr[i].name);
printf("Enter last name: ");
scanf("%s", ptr[i].surname);
printf("Enter semester: ");
scanf("%d", &ptr[i].semester);
printf("Enter grade: ");
scanf("%f", &ptr[i].grade);
}
}
else if (x==2)
{
ptr=realloc(ptr,100* sizeof(student));
std=std+1;
ptr[std].id=std+1;
printf("Enter first name: ");
scanf("%s", ptr[std].name);
printf("Enter last name: ");
scanf("%s", ptr[std].surname);
printf("Enter semester: ");
scanf("%d", &ptr[std].semester);
printf("Enter grade: ");
scanf("%f", &ptr[std].grade);
flag=0;
}
else if (x==3)
{
/* code */
}
else if (x==4)
{
printf("Give the surname: ");
const char *sur;
scanf("%s", sur);
for (i = 0; i < std; i++)
{
if(strcmp(sur, ptr[i].surname)==0){
printf("%d ", ptr[i].id);
printf("%s ", ptr[i].name);
printf("%s ", ptr[i].surname);
printf("%d ", ptr[i].semester);
printf("%f ", ptr[i].grade);
printf("\n");
car=1;
}
}
if (car=0)
{
printf("That surname does not exist");
}
}
I may have missed a "{" but this is only part of my code
else if (x==7)
{
for(i = 0; i < std; i++){
printf("%d ", ptr[i].id);
printf("%s ", ptr[i].name);
printf("%s ", ptr[i].surname);
printf("%d ", ptr[i].semester);
printf("%f ", ptr[i].grade);
printf("\n");
}
}
}
}
For when the user chooses "2" everything goes well until its time to print so i tried increasing std by 1 two times.One when the program goes to option "2" and another time when it goes back to main. It didnt do anything so i returned it to how it was in the beginning.
For when the user chooses "4" I tried going with strcmp but it would just throw errors because of sur. I had it written as char sur. After I wrote it as const char *sur it stopped throwing errors but it would just crash when it went to compare it with the other.

invalid type argument of '->' editing fields of a structure from a separate function

I'm having problems editing the fields of a structure from a seperate function, I'm trying to edit fields of my drone structure from the update droneinfofunction .basically i get the same error for all the arrows (invalid type argument of '->')
i'm sure this problem stems from my lack of understanding of pointers
any help would be greatly appreciated :)
here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DRONE_COUNT 10
typedef struct{
//define struct info and variables
int drone_number;
char drone_name[20];
int year_manufactured;
double mass;
double top_speed;
double max_distance;
double load_capacity;
} drone_info;
int updateDroneInfo(drone_info *droneinfo, int no_of_drones) {
int searchID, numdrones, i, drYrMan;
double drMass, drTopSpeed, drMaxDist, drLoadCap;
char drname[20];
numdrones = no_of_drones;
printf("what drone ID would you like to update?: ");
scanf("%d", &searchID);
printf("name: ");
scanf("%s", drname);
printf("year manufactured: ");
scanf("%d", &drYrMan);
printf("mass: ");
scanf("%lf", &drMass);
printf("top speed: ");
scanf("%lf", &drTopSpeed);
printf("max distance: ");
scanf("%lf", &drMaxDist);
printf("load capacity: ");
scanf("%lf", &drLoadCap);
droneinfo[searchID]->drone_number = searchID;
droneinfo[searchID]->drone_name = drname;
droneinfo[searchID]->year_manufactured = drYrMan;
droneinfo[searchID]->mass = drMass;
droneinfo[searchID]->top_speed = drTopSpeed;
droneinfo[searchID]->max_distance = drMaxDist;
droneinfo[searchID]->load_capacity = drLoadCap;
for(i=0; i < numdrones; i++){
}
return 0;
}
//drone search function
int searchDroneName(drone_info *droneinfo, int no_of_drones){
int i, found;
char namechoice[20];
printf("input drone name: ");
scanf("%s", namechoice);
found=0;
scanf("what drone would you like to search %s", namechoice);
for (i=0; i < no_of_drones; i++){
if (!strcmp(namechoice, droneinfo[i].drone_name)) {
printf("found a match\n\nID: %d Name: %s Year: %d Mass: %.2f Top Speed: %.2f Max Distance: %.2f Load Capacity: %.2f\n",
droneinfo[i].drone_number, droneinfo[i].drone_name, droneinfo[i].year_manufactured, droneinfo[i].mass, droneinfo[i].top_speed, droneinfo[i].max_distance, droneinfo[i].load_capacity);
found = 1;
}
}
if(found == 0){
printf("\nNo matches were found!\n");
}
return 0;
//make condition for all
}
int main(void) {
drone_info droneinfo[10];
int choice, droneID, yrman, i, no_of_drones;
double dronemass, dronemaxdist, dronetopspd, droneload;
char dronename[20];
i=0;
//open the drone.txt file where the drone info is stored
FILE* inputfile = fopen("drone.txt", "r");
if(inputfile == NULL)
{
perror("ERROR! ");
exit(-1);
}
//initialise the function that puts the struct in an array
while(fscanf(inputfile, "%d %19s %d %lf %lf %lf %lf", &droneID, dronename, &yrman, &dronemass, &dronetopspd, &dronemaxdist, &droneload)==7){
if(ferror(inputfile)){
perror("An error occurred: ");
}
droneinfo[i].drone_number = droneID;
strcpy(droneinfo[i].drone_name, dronename);
droneinfo[i].year_manufactured = yrman;
droneinfo[i].mass = dronemass;
droneinfo[i].top_speed = dronetopspd;
droneinfo[i].max_distance = dronemaxdist;
droneinfo[i].load_capacity = droneload;
i++;
}
no_of_drones = i;
fclose(inputfile);
//print the dtone info in an array
printf("Data:\n\n");
for (i=0; i < no_of_drones; i++){
printf("ID: %d Name: %s Year: %d Mass: %.2f Top Speed: %.2f Max Distance: %.2f Load Capacity: %.2f\n",
droneinfo[i].drone_number, droneinfo[i].drone_name, droneinfo[i].year_manufactured, droneinfo[i].mass, droneinfo[i].top_speed, droneinfo[i].max_distance, droneinfo[i].load_capacity);
}
do{
//program menu with appropriate menu items
printf("Please select an option:\n\n");
printf("1. Input/update drone information\n");
printf("2. Search a drone\n");
printf("3. Simulate a drone delivery scenario\n");
printf("4. Display simulation results\n");
printf("5. Save drone information\n");
printf("6. Save all results\n");
printf("7. Exit\n\n");
scanf("%d", &choice);
//switch for the 7 available menu cases
switch(choice)
{
case 1:
//input drone function
updateDroneInfo(droneinfo, no_of_drones);
break;
case 2:
//search drone function
searchDroneName(droneinfo, no_of_drones);
break;
case 3:
//simulate drone function
break;
case 4:
//display simulation results
break;
case 5:
//save drone information
break;
case 6:
//save all results function
break;
case 7:
//exit/breaks the loop
break;
default:
printf("Invalid Data Entered! please enter a number between 1 and 7\n\n");
break;
}
} while(choice != 7);
return 0;
}
re
droneinfo[searchID]
has the type drone_info and not drone_info*, so you use . instead of ->.
In statements like this:
droneinfo[searchID]->drone_number = searchID;
the expression droneinfo[searchID] is not a pointer. It has the type drone_info because the pointer droneinfo was already dereferenced by the subscript operator.
You have to write:
droneinfo[searchID].drone_number = searchID;
Also arrays do not have the assignment operator. You need to copy element elements from one array to another.
Instead of this statement:
droneinfo[searchID]->drone_name = drname;
you have to write using the standard string function strcpy:
$include <string.h>
//...
strcpy( droneinfo[searchID].drone_name, drname );

scanf in function seems to be writing garbage values to one array but not the other, despite both being identical

So in the function getRoster, I have two arrays that are defined in the same way in main and called in the same way. But for some reason, one of the scanf functions write garbage data to the array so that in line 91 (A debugging line in this case) one value is the one I entered and the other is seemingly random. I've checked this code top to bottom already to see if there were any notations that I added to one array and not the other, and I can't seem to find a single one.
#include <stdio.h>
#include <stdlib.h>
void getRoster(int *jerseyNumbers[10], int *playerRatings[10])
{
int i;
for (i=0; i<5; ++i)
{
printf("Enter player %d's jersey number\n", i+1);
scanf("%d", &jerseyNumbers[i]);
printf("Enter player %d's rating\n", i+1);
scanf("%d", &playerRatings[i]);
}
}
void updateRating(int *jerseyNumbers[10], int *playerRatings[10])
{
int i, n = -1, hold;
printf("Enter a jersey number:\n");
scanf("%d", n);
for (i=0; i<5; ++i)
if(jerseyNumbers[i]==n)
hold = n;
if (n=-1);
printf("Error");
printf("Enter a new rating for player:\n");
scanf("%d", &playerRatings[n]);
}
void aboveRating()
{
}
void replacePlayer()
{
}
void outputRoster(int *jerseyNumbers[10], int *playerRatings[10])
{
int i;
for (i=0; i<5; ++i)
{
printf("Player %d -- Jersey number: %d , Rating: %d \n", i+1, jerseyNumbers[i], playerRatings[i]);
}
}
void menu(int *jerseyNumbers[10], int *playerRatings[10])
{
char menuInput;
printf("\nMENU\n");
printf("u - Update player rating\n");
printf("a - Output players above a rating\n");
printf("r - Replace player\n");
printf("o - Output roster\n");
printf("q - Quit\n");
printf("\nChoose an option:\n");
scanf(" %c", &menuInput);
if (menuInput == 'u')
{updateRating(&jerseyNumbers[10], &playerRatings[10]);
}
else if (menuInput == 'a')
{aboveRating();
}
else if (menuInput == 'r')
{replacePlayer();
}
else if (menuInput == 'o')
{outputRoster(&jerseyNumbers[10], &playerRatings[10]);
}
else if (menuInput == 'q')
printf("Reached Quit");
else
printf("Input Error\n");
}
int main()
{
int *jerseyNumbers[10];
int *playerRatings[10];
char menuInput;
int quitFlag = 0;
getRoster(&jerseyNumbers[10], &playerRatings[10]);
printf("%d %d", jerseyNumbers[0], playerRatings[1]);
menu(&jerseyNumbers[10], &playerRatings[10]);
return 0;
}
Refer to #NaveenKumar and #DeiDei's comment to your question.
Actually, the logic of your program is alright. But where you've gone wrong is the syntax regarding how you have declared the arrays and passed them as arguments to the functions. I have listed the changes to be made below:
First of all Dylan, do not declare an array as: int
*jerseyNumbers[10] and int *playerRatings[10]. For the regular array which you actually need, just declare as:
a. *jerseyNumbers and *playerRatings... Remove the size. OR
b. jerseyNumbers[10] and playerRatings[10]... Remove the * from the declaration.
When you're passing these arrays as arguments to a function, don't ever send it how you've done. Just pass the array name as an argument. Like this: getRoster(jerseyNumbers, playerRatings); and menu(jerseyNumbers, playerRatings);.
In the function definition, the parameters representing the array should be either *arrayname or arrayname[size]. Since we have used the first during declaration, use the same here as follows:
void getRoster(int *jerseyNumbers, int *playerRatings) {...},
void updateRating(int *jerseyNumbers, int *playerRatings){...},
void outputRoster(int *jerseyNumbers, int *playerRatings) {...},
void menu(int *jerseyNumbers, int *playerRatings) {...}.
And as #PaulSm4 has suggested don't use the semi-colon at the end of an if conditional. Though this isn't the cause of your problem here, it is a practice you need to follow and mistake to be avoided.
I have attached the working code below, along with the output.
CODE:
#include <stdio.h>
#include <stdlib.h>
void getRoster(int *jerseyNumbers, int *playerRatings)
{
int i;
for (i=0; i<5; ++i)
{
printf("Enter player %d's jersey number\n", i+1);
scanf("%d", &jerseyNumbers[i]);
printf("Enter player %d's rating\n", i+1);
scanf("%d", &playerRatings[i]);
}
}
void updateRating(int *jerseyNumbers, int *playerRatings)
{
int i, n = -1, hold;
printf("Enter a jersey number:\n");
scanf("%d", n);
for (i=0; i<5; ++i)
if(jerseyNumbers[i]==n)
hold = n;
if (n=-1)
printf("Error");
printf("Enter a new rating for player:\n");
scanf("%d", &playerRatings[n]);
}
void aboveRating()
{
}
void replacePlayer()
{
}
void outputRoster(int *jerseyNumbers, int *playerRatings)
{
int i;
for (i=0; i<5; ++i)
{
printf("Player %d -- Jersey number: %d , Rating: %d \n", i+1, jerseyNumbers[i], playerRatings[i]);
}
}
void menu(int *jerseyNumbers, int *playerRatings)
{
char menuInput;
printf("\nMENU\n");
printf("u - Update player rating\n");
printf("a - Output players above a rating\n");
printf("r - Replace player\n");
printf("o - Output roster\n");
printf("q - Quit\n");
printf("\nChoose an option:\n");
scanf(" %c", &menuInput);
if (menuInput == 'u')
{updateRating(jerseyNumbers, playerRatings);
}
else if (menuInput == 'a')
{aboveRating();
}
else if (menuInput == 'r')
{replacePlayer();
}
else if (menuInput == 'o')
{outputRoster(jerseyNumbers, playerRatings);
}
else if (menuInput == 'q')
printf("Reached Quit");
else
printf("Input Error\n");
}
int main()
{
int jerseyNumbers[10];
int playerRatings[10];
char menuInput;
int quitFlag = 0;
getRoster(jerseyNumbers, playerRatings);
printf("%d %d", jerseyNumbers[0], playerRatings[1]);
menu(jerseyNumbers, playerRatings);
return 0;
}
OUTPUT:
Enter player 1's jersey number
1
Enter player 1's rating
10
Enter player 2's jersey number
2
Enter player 2's rating
20
Enter player 3's jersey number
3
Enter player 3's rating
30
Enter player 4's jersey number
4
Enter player 4's rating
40
Enter player 5's jersey number
5
Enter player 5's rating
50
1 20
MENU
u - Update player rating
a - Output players above a rating
r - Replace player
o - Output roster
q - Quit
Choose an option: o
Player 1 -- Jersey number: 1 , Rating: 10
Player 2 -- Jersey number: 2 , Rating: 20
Player 3 -- Jersey number: 3 , Rating: 30
Player 4 -- Jersey number: 4 , Rating: 40
Player 5 -- Jersey number: 5 , Rating: 50
Hope this helps.
STRONG SUGGESTION:
Get in the habit of using curly braces often - even when you don't need them.
void updateRating(int *jerseyNumbers, int *playerRatings)
// You probably only need a pointer or an array - but probably not both ;)
{
int i, n = -1, hold;
printf("Enter a jersey number:\n");
scanf("%d", n);
for (i=0; i<5; ++i) { // Curly brace here...
if(jerseyNumbers[i]==n) { // And here...
hold = n;
}
}
if (n=-1) { // BUG ALERT: deleted ";" (and added a brace)
printf("Error"); // BUG ALERT: fixed this by removing ";"?
} else {
printf("Enter a new rating for player:\n");
scanf("%d", &playerRatings[n]);
}
Or perhaps better:
void updateRating(int *jerseyNumbers, int *playerRatings)
{
int n;
printf("Enter a jersey number:\n");
scanf("%d", n);
printf("Enter a new rating for player:\n");
scanf("%d", &playerRatings[n]);
}
And, as Rishikesh Raje correctly pointed out:
The definition of the array's and the function calls for OP is also
not correct. Please mention this also.

Looping function on my int isMember()

#include <stdio.h>
int isMember();
float calculatePrice(int,int,int);
void printPlayer();
char name[4][100];
char name1[100];
int numofplayer, i, numofbook;
int member;
float game, discount, gst, subtotal, total;
int main()
{
i=0;
printf ("Your name: ");
gets (name1);
printf ("No of player: ");
scanf ("%d",&numofplayer);
while (i<numofplayer)
{
printf ("Enter Player %d: ",i+1);
scanf ("%s",name[i]);
i++;
}
printf ("How many game do you want to book?: ");
scanf ("%d",&numofbook);
isMember();
calculatePrice(isMember(),numofbook,numofplayer);
printf ("=======================\n");
printf ("Your booking detail.\n");
printf ("Your name is: %s",name1);
printf ("\nNo of player: %d",numofplayer);
printf ("\nList of player ");
printPlayer();
printf ("\nNo of game: %d",numofbook);
if (isMember()==1)
{
printf ("Status member: member \n");
}
else
{
printf ("Status member: not a member \n");
}
printf ("Total price is (including GST): %.2f",calculatePrice(isMember(),numofbook,numofplayer));
return 0;
}
int isMember()
{
printf ("Are you a member of this club?(1-yes or 0=no): ");
scanf ("%d",&member);
return member;
}
float calculatePrice(int a, int b, int c)
{
game=25;
subtotal=game*c;
subtotal=subtotal*b;
if (a==1)
{
discount=subtotal*0.1;
subtotal=subtotal-discount;
}
gst=0.06*subtotal;
total=subtotal-gst;
return total;
}
void printPlayer()
{
int i;
i=0;
while (i<numofplayer)
{
printf ("%d) ",i+1);
printf ("%s \n",name[i]);
i++;
}
}
Why did it loop at my isMember()?
It keeps looping over and over then it stops, and the other function I am not even confident if it runs correctly.
It doesn't loop.
Instead of keeping the returned value from isMember(), you called it over and over again.
You should define a new variable and save the result in it.
res = isMember();
and use res anywhere you called isMember().

Why only else statement is working, if statement is not executing in my code? [duplicate]

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.

Resources