Why does my program keep printing garbage values?How do I print the full value of a long string in gdb?Why does printf not flush after the call unless a newline is in the format string?How can I print the value in this stackT?Why does sizeof(x++) not increment x?Why does a function with no parameters (compared to the actual function definition) compile?Why does the C preprocessor interpret the word “linux” as the constant “1”?Why does ENOENT mean “No such file or directory”?Why does this program print “forked!” 4 times?Why does rand() + rand() produce negative numbers?Why not Garbage Value is being printed by this program
Boots or trail runners with reference to blisters?
Is it unprofessional to mention your cover letter and resume are best viewed in Chrome?
Should I intervene when a colleague in a different department makes students run laps as part of their grade?
What would the United Kingdom's "optimal" Brexit deal look like?
Is there a word or phrase that means 'works but not for the reason we expect it to'?
How can I convert a linear narrative into a branching narrative?
Why put copper in between battery contacts and clamps?
Would people understand me speaking German all over Europe?
Is it okay for me to decline a project on ethical grounds?
Move arrows along a contour
Easy way to get process from window
Rampant sharing of authorship among colleagues in the name of "collaboration". Is not taking part in it a death knell for a future in academia?
Was Donald Trump at ground zero helping out on 9-11?
"Valet parking " or "parking valet"
Solve equation using Mathematica
What force enables us to walk? Friction or normal reaction?
How can a class have multiple methods without breaking the single responsibility principle
Word for giving preference to the oldest child
Should 2FA be enabled on service accounts?
How can a circuit not have a neutral?
Bouncing map back into its bounds, after user dragged it out
Just how much information should you share with a former client?
Security measures that could plausibly last 150+ years?
Why does the Rust compiler not optimize code assuming that two mutable references cannot alias?
Why does my program keep printing garbage values?
How do I print the full value of a long string in gdb?Why does printf not flush after the call unless a newline is in the format string?How can I print the value in this stackT?Why does sizeof(x++) not increment x?Why does a function with no parameters (compared to the actual function definition) compile?Why does the C preprocessor interpret the word “linux” as the constant “1”?Why does ENOENT mean “No such file or directory”?Why does this program print “forked!” 4 times?Why does rand() + rand() produce negative numbers?Why not Garbage Value is being printed by this program
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
For some reason when i input my time , description and index and click quit to print the array that i have populated with values and strings, i get a bunch of garbage values printed instead of what i inputed, not sure what im doing wrong. any advice?
For some reason when i input my time , description and index and click quit to print the array that i have populated with values and strings, i get a bunch of garbage values printed instead of what i inputed, not sure what im doing wrong. any advice?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 5
struct event //data type called event which holds time and description for events
int hour; //holds the hour digit between 0-23
int minute; //holds the minute digit between 0-59
char description[41];
//holds the description for the reason of the alarm
;
typedef struct event Event; //allows coder to only have to use "Event" instead of "struct event"
int InputRange(int min, int max);
Event* InputEvent(Event *newEvent);
int AddEventAtIndex(Event list[], Event e, int i);
//int InsertionSortEvent(Event list[], int *p_size, Event e);
//void DisplayEvent(Event e);
//void DisplayEventList(Event list[], int size);
//int DeleteEvent(Event list[], int i, int *p_size);
int main (void)
Event EventList[MAX];
Event e;
int i=0;
int eventListSize = 0;
int choice;
do
printf("__= Scheduler v1.0 =__n");
printf("1. Schedule an event.n");
printf("2. Delete an event.n");
printf("3. Display schedule.n");
printf("4. Save schedule.n");
printf("5. Load schedule.n");
printf("6. Exitn");
switch(choice = InputRange(1, 6))
case 1: InputEvent( EventList );
i = AddEventAtIndex(EventList, e, i);
break;
/* case 2: pHead = deleteStudent(pHead);
break;
case 3: printf("Press 1 to search by ID or 2 to search by name: n");
scanf("%d", &search);
if (search == 1)
searchStudentID(pHead);
else if (search == 2)
searchStudentlName(pHead);
else
printf("Invalid selection");
break;
case 4: displayStudentInfo(pHead);
break;
case 5: saveStudentInfo(pHead);
break;
case 6: end(pHead);
break;*/
default: printf("Exiting Programnn");
while ( choice != 6 );
printf("Index #[]tTimetDescription");
for ( int j = 1; j < 6; j++)
printf("n[%d]nt%d:%d t %s", j, EventList[j].hour,
EventList[j].minute, EventList[j].description);
int InputRange(int min, int max)
int timenumber;
printf("Please enter a number between %d - %dn", min, max);
scanf("%d", &timenumber);
printf("n");
if (timenumber < min
Event* InputEvent(Event *newEvent)
if (newEvent != NULL) // quality assurance:
// make sure the pointer is valid
printf("Enter the event time:n");
newEvent->hour = InputRange(0, 23);
newEvent->minute = InputRange(0, 59);
printf("Enter the event description:n");
fgetc(stdin);
fgets(newEvent->description, 41, stdin);
printf("n");
return newEvent;
int AddEventAtIndex(Event list[], Event e, int i)
--i;
printf("Where in the array would you like to store this eventn");
i = InputRange(1, 5);
list[i].hour = e.hour;
list[i].minute = e.minute;
strcpy(list[i].description, e.description);
return i;
c
add a comment |
For some reason when i input my time , description and index and click quit to print the array that i have populated with values and strings, i get a bunch of garbage values printed instead of what i inputed, not sure what im doing wrong. any advice?
For some reason when i input my time , description and index and click quit to print the array that i have populated with values and strings, i get a bunch of garbage values printed instead of what i inputed, not sure what im doing wrong. any advice?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 5
struct event //data type called event which holds time and description for events
int hour; //holds the hour digit between 0-23
int minute; //holds the minute digit between 0-59
char description[41];
//holds the description for the reason of the alarm
;
typedef struct event Event; //allows coder to only have to use "Event" instead of "struct event"
int InputRange(int min, int max);
Event* InputEvent(Event *newEvent);
int AddEventAtIndex(Event list[], Event e, int i);
//int InsertionSortEvent(Event list[], int *p_size, Event e);
//void DisplayEvent(Event e);
//void DisplayEventList(Event list[], int size);
//int DeleteEvent(Event list[], int i, int *p_size);
int main (void)
Event EventList[MAX];
Event e;
int i=0;
int eventListSize = 0;
int choice;
do
printf("__= Scheduler v1.0 =__n");
printf("1. Schedule an event.n");
printf("2. Delete an event.n");
printf("3. Display schedule.n");
printf("4. Save schedule.n");
printf("5. Load schedule.n");
printf("6. Exitn");
switch(choice = InputRange(1, 6))
case 1: InputEvent( EventList );
i = AddEventAtIndex(EventList, e, i);
break;
/* case 2: pHead = deleteStudent(pHead);
break;
case 3: printf("Press 1 to search by ID or 2 to search by name: n");
scanf("%d", &search);
if (search == 1)
searchStudentID(pHead);
else if (search == 2)
searchStudentlName(pHead);
else
printf("Invalid selection");
break;
case 4: displayStudentInfo(pHead);
break;
case 5: saveStudentInfo(pHead);
break;
case 6: end(pHead);
break;*/
default: printf("Exiting Programnn");
while ( choice != 6 );
printf("Index #[]tTimetDescription");
for ( int j = 1; j < 6; j++)
printf("n[%d]nt%d:%d t %s", j, EventList[j].hour,
EventList[j].minute, EventList[j].description);
int InputRange(int min, int max)
int timenumber;
printf("Please enter a number between %d - %dn", min, max);
scanf("%d", &timenumber);
printf("n");
if (timenumber < min
Event* InputEvent(Event *newEvent)
if (newEvent != NULL) // quality assurance:
// make sure the pointer is valid
printf("Enter the event time:n");
newEvent->hour = InputRange(0, 23);
newEvent->minute = InputRange(0, 59);
printf("Enter the event description:n");
fgetc(stdin);
fgets(newEvent->description, 41, stdin);
printf("n");
return newEvent;
int AddEventAtIndex(Event list[], Event e, int i)
--i;
printf("Where in the array would you like to store this eventn");
i = InputRange(1, 5);
list[i].hour = e.hour;
list[i].minute = e.minute;
strcpy(list[i].description, e.description);
return i;
c
3
Provided code does not compile. Start by formatting the code with proper indents, fixing compile errors, and then recompiling with the maximum warning level and correcting warnings produced by the compiler.
– Reticulated Spline
Mar 26 at 21:43
You have several problems in your program, see my answer listing them more a proposal to solve them and an example of execution
– bruno
Mar 26 at 23:10
add a comment |
For some reason when i input my time , description and index and click quit to print the array that i have populated with values and strings, i get a bunch of garbage values printed instead of what i inputed, not sure what im doing wrong. any advice?
For some reason when i input my time , description and index and click quit to print the array that i have populated with values and strings, i get a bunch of garbage values printed instead of what i inputed, not sure what im doing wrong. any advice?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 5
struct event //data type called event which holds time and description for events
int hour; //holds the hour digit between 0-23
int minute; //holds the minute digit between 0-59
char description[41];
//holds the description for the reason of the alarm
;
typedef struct event Event; //allows coder to only have to use "Event" instead of "struct event"
int InputRange(int min, int max);
Event* InputEvent(Event *newEvent);
int AddEventAtIndex(Event list[], Event e, int i);
//int InsertionSortEvent(Event list[], int *p_size, Event e);
//void DisplayEvent(Event e);
//void DisplayEventList(Event list[], int size);
//int DeleteEvent(Event list[], int i, int *p_size);
int main (void)
Event EventList[MAX];
Event e;
int i=0;
int eventListSize = 0;
int choice;
do
printf("__= Scheduler v1.0 =__n");
printf("1. Schedule an event.n");
printf("2. Delete an event.n");
printf("3. Display schedule.n");
printf("4. Save schedule.n");
printf("5. Load schedule.n");
printf("6. Exitn");
switch(choice = InputRange(1, 6))
case 1: InputEvent( EventList );
i = AddEventAtIndex(EventList, e, i);
break;
/* case 2: pHead = deleteStudent(pHead);
break;
case 3: printf("Press 1 to search by ID or 2 to search by name: n");
scanf("%d", &search);
if (search == 1)
searchStudentID(pHead);
else if (search == 2)
searchStudentlName(pHead);
else
printf("Invalid selection");
break;
case 4: displayStudentInfo(pHead);
break;
case 5: saveStudentInfo(pHead);
break;
case 6: end(pHead);
break;*/
default: printf("Exiting Programnn");
while ( choice != 6 );
printf("Index #[]tTimetDescription");
for ( int j = 1; j < 6; j++)
printf("n[%d]nt%d:%d t %s", j, EventList[j].hour,
EventList[j].minute, EventList[j].description);
int InputRange(int min, int max)
int timenumber;
printf("Please enter a number between %d - %dn", min, max);
scanf("%d", &timenumber);
printf("n");
if (timenumber < min
Event* InputEvent(Event *newEvent)
if (newEvent != NULL) // quality assurance:
// make sure the pointer is valid
printf("Enter the event time:n");
newEvent->hour = InputRange(0, 23);
newEvent->minute = InputRange(0, 59);
printf("Enter the event description:n");
fgetc(stdin);
fgets(newEvent->description, 41, stdin);
printf("n");
return newEvent;
int AddEventAtIndex(Event list[], Event e, int i)
--i;
printf("Where in the array would you like to store this eventn");
i = InputRange(1, 5);
list[i].hour = e.hour;
list[i].minute = e.minute;
strcpy(list[i].description, e.description);
return i;
c
For some reason when i input my time , description and index and click quit to print the array that i have populated with values and strings, i get a bunch of garbage values printed instead of what i inputed, not sure what im doing wrong. any advice?
For some reason when i input my time , description and index and click quit to print the array that i have populated with values and strings, i get a bunch of garbage values printed instead of what i inputed, not sure what im doing wrong. any advice?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 5
struct event //data type called event which holds time and description for events
int hour; //holds the hour digit between 0-23
int minute; //holds the minute digit between 0-59
char description[41];
//holds the description for the reason of the alarm
;
typedef struct event Event; //allows coder to only have to use "Event" instead of "struct event"
int InputRange(int min, int max);
Event* InputEvent(Event *newEvent);
int AddEventAtIndex(Event list[], Event e, int i);
//int InsertionSortEvent(Event list[], int *p_size, Event e);
//void DisplayEvent(Event e);
//void DisplayEventList(Event list[], int size);
//int DeleteEvent(Event list[], int i, int *p_size);
int main (void)
Event EventList[MAX];
Event e;
int i=0;
int eventListSize = 0;
int choice;
do
printf("__= Scheduler v1.0 =__n");
printf("1. Schedule an event.n");
printf("2. Delete an event.n");
printf("3. Display schedule.n");
printf("4. Save schedule.n");
printf("5. Load schedule.n");
printf("6. Exitn");
switch(choice = InputRange(1, 6))
case 1: InputEvent( EventList );
i = AddEventAtIndex(EventList, e, i);
break;
/* case 2: pHead = deleteStudent(pHead);
break;
case 3: printf("Press 1 to search by ID or 2 to search by name: n");
scanf("%d", &search);
if (search == 1)
searchStudentID(pHead);
else if (search == 2)
searchStudentlName(pHead);
else
printf("Invalid selection");
break;
case 4: displayStudentInfo(pHead);
break;
case 5: saveStudentInfo(pHead);
break;
case 6: end(pHead);
break;*/
default: printf("Exiting Programnn");
while ( choice != 6 );
printf("Index #[]tTimetDescription");
for ( int j = 1; j < 6; j++)
printf("n[%d]nt%d:%d t %s", j, EventList[j].hour,
EventList[j].minute, EventList[j].description);
int InputRange(int min, int max)
int timenumber;
printf("Please enter a number between %d - %dn", min, max);
scanf("%d", &timenumber);
printf("n");
if (timenumber < min
Event* InputEvent(Event *newEvent)
if (newEvent != NULL) // quality assurance:
// make sure the pointer is valid
printf("Enter the event time:n");
newEvent->hour = InputRange(0, 23);
newEvent->minute = InputRange(0, 59);
printf("Enter the event description:n");
fgetc(stdin);
fgets(newEvent->description, 41, stdin);
printf("n");
return newEvent;
int AddEventAtIndex(Event list[], Event e, int i)
--i;
printf("Where in the array would you like to store this eventn");
i = InputRange(1, 5);
list[i].hour = e.hour;
list[i].minute = e.minute;
strcpy(list[i].description, e.description);
return i;
c
c
edited Mar 26 at 23:08
Reticulated Spline
3421 silver badge12 bronze badges
3421 silver badge12 bronze badges
asked Mar 26 at 21:35
Michael BachaMichael Bacha
32 bronze badges
32 bronze badges
3
Provided code does not compile. Start by formatting the code with proper indents, fixing compile errors, and then recompiling with the maximum warning level and correcting warnings produced by the compiler.
– Reticulated Spline
Mar 26 at 21:43
You have several problems in your program, see my answer listing them more a proposal to solve them and an example of execution
– bruno
Mar 26 at 23:10
add a comment |
3
Provided code does not compile. Start by formatting the code with proper indents, fixing compile errors, and then recompiling with the maximum warning level and correcting warnings produced by the compiler.
– Reticulated Spline
Mar 26 at 21:43
You have several problems in your program, see my answer listing them more a proposal to solve them and an example of execution
– bruno
Mar 26 at 23:10
3
3
Provided code does not compile. Start by formatting the code with proper indents, fixing compile errors, and then recompiling with the maximum warning level and correcting warnings produced by the compiler.
– Reticulated Spline
Mar 26 at 21:43
Provided code does not compile. Start by formatting the code with proper indents, fixing compile errors, and then recompiling with the maximum warning level and correcting warnings produced by the compiler.
– Reticulated Spline
Mar 26 at 21:43
You have several problems in your program, see my answer listing them more a proposal to solve them and an example of execution
– bruno
Mar 26 at 23:10
You have several problems in your program, see my answer listing them more a proposal to solve them and an example of execution
– bruno
Mar 26 at 23:10
add a comment |
1 Answer
1
active
oldest
votes
Why does my program keep printing garbage values?
You use non initialized data, in main you have :
Event e;
...
i = AddEventAtIndex(EventList, e, i);
where e is not initialized but used in AddEventAtIndex :
int AddEventAtIndex(Event list[], Event e, int i)
{
...
list[i].hour = e.hour;
list[i].minute = e.minute;
strcpy(list[i].description, e.description);
the behavior of strcpy with an non initialized value is undefined and can have a dramatic consequences.
Probably the call InputEvent( EventList );
must be replaced by InputEvent(&e);
In
for ( int j = 1 ; j < 6 ; j++)
printf("n[%d]nt%d:%d t %s", j, EventList[j].hour,
EventList[j].minute, EventList[j].description);
You access out of EventList when j is 5 and 6, do for instance :
for ( int j = 0 ; j < MAX ; j++)
printf("[%d]t%d:%d t %sn", j+1, EventList[j].hour,
EventList[j].minute, EventList[j].description);
But you also write entries never set printing 'garbage values', more the fact to print a string non initialized has undefined effect. A way to separate entries set and not set is to initialize all the hours by 24 and to test that value in the loop to write or not the entry.
I also changed the format because the newline after the index is not compatible with printf("Index #[]tTimetDescription");
and it is better to put the other newline after all rather than before to flush the output line, so also change printf("Index #[]tTimetDescription");
by puts("Index #[]tTimetDescription");
MAX is not a very good name because it is more a SUP, or just rename it by SIZE
In main you do not use eventListSize
In AddEventAtIndex
--i
must be done after i = InputRange(1, 5);
or just do i = InputRange(1, 5) - 1;
else when i values 5 list[i]
is out of list so you write out of list with an undefined behavior.
So finally the parameter i is useless, remove it.
The return index is also not used in main, you use it to assign i and do not used the value of i after. AddEventAtIndex do not need to return a value.
In InputRange
You do not check the result of scanf so the value of timenumber is undefined if a non valid integer was enter and if you do not purge the input so all the next scanf to get a number will not success
Also if the value is not in the range you just call InputRange(min, max);
without returning its value so you finally return the invalid value, put all in a for(;;)
for instance to only return when the value is correct.
You use fgets in InputEvent, to mix it with scanf to read number is a source of problem, replace the scanf by the use of fgets then a sscanf on the read line
In InputEvent
When you do fgetc(stdin);
you probably hope to bypass a newline but if the user enter characters after the number in InputRange (supposing a number was enter) fgets will read the first of them and fgets will not get the expected description. The solution is to do fgets then sscanf in InputRange as I said above allowing you to remove that fgetc
A proposal taking into account my remarks :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 5
struct event //data type called event which holds time and description for events
int hour; //holds the hour digit between 0-23
int minute; //holds the minute digit between 0-59
char description[41]; //holds the description for the reason of the alarm
;
typedef struct event Event; //allows coder to only have to use "Event" instead of "struct event"
int InputRange(int min, int max);
Event* InputEvent(Event *newEvent);
void AddEventAtIndex(Event list[], Event e);
//int InsertionSortEvent(Event list[], int *p_size, Event e);
//void DisplayEvent(Event e);
//void DisplayEventList(Event list[], int size);
//int DeleteEvent(Event list[], int i, int *p_size);
int main (void)
Event EventList[MAX];
Event e;
int choice;
/* mark uset entries */
for (int i = 0; i != MAX; ++i)
EventList[i].hour = 24;
do
printf("__= Scheduler v1.0 =__n");
printf("1. Schedule an event.n");
printf("2. Delete an event.n");
printf("3. Display schedule.n");
printf("4. Save schedule.n");
printf("5. Load schedule.n");
printf("6. Exitn");
switch(choice = InputRange(1, 6))
case 1:
InputEvent( &e );
AddEventAtIndex(EventList, e);
break;
/*case 2: pHead = deleteStudent(pHead);
break;
case 3: printf("Press 1 to search by ID or 2 to search by name: n");
scanf("%d", &search);
if (search == 1)
searchStudentID(pHead);
else if (search == 2)
searchStudentlName(pHead);
else
printf("Invalid selection");
break;
case 4: displayStudentInfo(pHead);
break;
case 5: saveStudentInfo(pHead);
break;
case 6: end(pHead);
break;*/
default: printf("Exiting Programnn");
while ( choice != 6 );
puts("Index #[]tTimetDescription");
for ( int j = 0 ; j < MAX ; j++)
if (EventList[j].hour != 24)
printf("t[%d]t%d:%d t %sn", j+1, EventList[j].hour,
EventList[j].minute, EventList[j].description);
int InputRange(int min, int max)
char line[32];
int timenumber;
for (;;)
printf("Please enter a number between %d - %dn", min, max);
if (fgets(line, sizeof(line), stdin) == NULL)
/* EOF */
exit(-1);
if ((sscanf(line, "%d", &timenumber) == 1) &&
(timenumber >= min) &&
(timenumber <= max))
return timenumber;
printf("Invalid Entryn");
Event* InputEvent(Event *newEvent)
if (newEvent != NULL) // quality assurance:
// make sure the pointer is valid
printf("Enter the event time:n");
newEvent->hour = InputRange(0, 23);
newEvent->minute = InputRange(0, 59);
printf("Enter the event description:n");
fgets(newEvent->description, sizeof(newEvent->description), stdin);
printf("n");
return newEvent;
void AddEventAtIndex(Event list[], Event e)
printf("Where in the array would you like to store this eventn");
int i = InputRange(1, 5) - 1;
list[i].hour = e.hour;
list[i].minute = e.minute;
strcpy(list[i].description, e.description);
Compilation and execution :
pi@raspberrypi:/tmp $ gcc -g -pedantic -Wextra -Wall c.c
pi@raspberrypi:/tmp $ ./a.out
__= Scheduler v1.0 =__
1. Schedule an event.
2. Delete an event.
3. Display schedule.
4. Save schedule.
5. Load schedule.
6. Exit
Please enter a number between 1 - 6
1
Enter the event time:
Please enter a number between 0 - 23
2
Please enter a number between 0 - 59
22
Enter the event description:
descr1
Where in the array would you like to store this event
Please enter a number between 1 - 5
1
__= Scheduler v1.0 =__
1. Schedule an event.
2. Delete an event.
3. Display schedule.
4. Save schedule.
5. Load schedule.
6. Exit
Please enter a number between 1 - 6
1
Enter the event time:
Please enter a number between 0 - 23
3
Please enter a number between 0 - 59
33
Enter the event description:
descr2
Where in the array would you like to store this event
Please enter a number between 1 - 5
4
__= Scheduler v1.0 =__
1. Schedule an event.
2. Delete an event.
3. Display schedule.
4. Save schedule.
5. Load schedule.
6. Exit
Please enter a number between 1 - 6
12
Invalid Entry
Please enter a number between 1 - 6
6
Exiting Program
Index #[] Time Description
[1] 2:22 descr1
[4] 3:33 descr2
note the empty line in the final print, this is because the newline is part of the description, it must be remove is present in InputEvent
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55366566%2fwhy-does-my-program-keep-printing-garbage-values%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Why does my program keep printing garbage values?
You use non initialized data, in main you have :
Event e;
...
i = AddEventAtIndex(EventList, e, i);
where e is not initialized but used in AddEventAtIndex :
int AddEventAtIndex(Event list[], Event e, int i)
{
...
list[i].hour = e.hour;
list[i].minute = e.minute;
strcpy(list[i].description, e.description);
the behavior of strcpy with an non initialized value is undefined and can have a dramatic consequences.
Probably the call InputEvent( EventList );
must be replaced by InputEvent(&e);
In
for ( int j = 1 ; j < 6 ; j++)
printf("n[%d]nt%d:%d t %s", j, EventList[j].hour,
EventList[j].minute, EventList[j].description);
You access out of EventList when j is 5 and 6, do for instance :
for ( int j = 0 ; j < MAX ; j++)
printf("[%d]t%d:%d t %sn", j+1, EventList[j].hour,
EventList[j].minute, EventList[j].description);
But you also write entries never set printing 'garbage values', more the fact to print a string non initialized has undefined effect. A way to separate entries set and not set is to initialize all the hours by 24 and to test that value in the loop to write or not the entry.
I also changed the format because the newline after the index is not compatible with printf("Index #[]tTimetDescription");
and it is better to put the other newline after all rather than before to flush the output line, so also change printf("Index #[]tTimetDescription");
by puts("Index #[]tTimetDescription");
MAX is not a very good name because it is more a SUP, or just rename it by SIZE
In main you do not use eventListSize
In AddEventAtIndex
--i
must be done after i = InputRange(1, 5);
or just do i = InputRange(1, 5) - 1;
else when i values 5 list[i]
is out of list so you write out of list with an undefined behavior.
So finally the parameter i is useless, remove it.
The return index is also not used in main, you use it to assign i and do not used the value of i after. AddEventAtIndex do not need to return a value.
In InputRange
You do not check the result of scanf so the value of timenumber is undefined if a non valid integer was enter and if you do not purge the input so all the next scanf to get a number will not success
Also if the value is not in the range you just call InputRange(min, max);
without returning its value so you finally return the invalid value, put all in a for(;;)
for instance to only return when the value is correct.
You use fgets in InputEvent, to mix it with scanf to read number is a source of problem, replace the scanf by the use of fgets then a sscanf on the read line
In InputEvent
When you do fgetc(stdin);
you probably hope to bypass a newline but if the user enter characters after the number in InputRange (supposing a number was enter) fgets will read the first of them and fgets will not get the expected description. The solution is to do fgets then sscanf in InputRange as I said above allowing you to remove that fgetc
A proposal taking into account my remarks :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 5
struct event //data type called event which holds time and description for events
int hour; //holds the hour digit between 0-23
int minute; //holds the minute digit between 0-59
char description[41]; //holds the description for the reason of the alarm
;
typedef struct event Event; //allows coder to only have to use "Event" instead of "struct event"
int InputRange(int min, int max);
Event* InputEvent(Event *newEvent);
void AddEventAtIndex(Event list[], Event e);
//int InsertionSortEvent(Event list[], int *p_size, Event e);
//void DisplayEvent(Event e);
//void DisplayEventList(Event list[], int size);
//int DeleteEvent(Event list[], int i, int *p_size);
int main (void)
Event EventList[MAX];
Event e;
int choice;
/* mark uset entries */
for (int i = 0; i != MAX; ++i)
EventList[i].hour = 24;
do
printf("__= Scheduler v1.0 =__n");
printf("1. Schedule an event.n");
printf("2. Delete an event.n");
printf("3. Display schedule.n");
printf("4. Save schedule.n");
printf("5. Load schedule.n");
printf("6. Exitn");
switch(choice = InputRange(1, 6))
case 1:
InputEvent( &e );
AddEventAtIndex(EventList, e);
break;
/*case 2: pHead = deleteStudent(pHead);
break;
case 3: printf("Press 1 to search by ID or 2 to search by name: n");
scanf("%d", &search);
if (search == 1)
searchStudentID(pHead);
else if (search == 2)
searchStudentlName(pHead);
else
printf("Invalid selection");
break;
case 4: displayStudentInfo(pHead);
break;
case 5: saveStudentInfo(pHead);
break;
case 6: end(pHead);
break;*/
default: printf("Exiting Programnn");
while ( choice != 6 );
puts("Index #[]tTimetDescription");
for ( int j = 0 ; j < MAX ; j++)
if (EventList[j].hour != 24)
printf("t[%d]t%d:%d t %sn", j+1, EventList[j].hour,
EventList[j].minute, EventList[j].description);
int InputRange(int min, int max)
char line[32];
int timenumber;
for (;;)
printf("Please enter a number between %d - %dn", min, max);
if (fgets(line, sizeof(line), stdin) == NULL)
/* EOF */
exit(-1);
if ((sscanf(line, "%d", &timenumber) == 1) &&
(timenumber >= min) &&
(timenumber <= max))
return timenumber;
printf("Invalid Entryn");
Event* InputEvent(Event *newEvent)
if (newEvent != NULL) // quality assurance:
// make sure the pointer is valid
printf("Enter the event time:n");
newEvent->hour = InputRange(0, 23);
newEvent->minute = InputRange(0, 59);
printf("Enter the event description:n");
fgets(newEvent->description, sizeof(newEvent->description), stdin);
printf("n");
return newEvent;
void AddEventAtIndex(Event list[], Event e)
printf("Where in the array would you like to store this eventn");
int i = InputRange(1, 5) - 1;
list[i].hour = e.hour;
list[i].minute = e.minute;
strcpy(list[i].description, e.description);
Compilation and execution :
pi@raspberrypi:/tmp $ gcc -g -pedantic -Wextra -Wall c.c
pi@raspberrypi:/tmp $ ./a.out
__= Scheduler v1.0 =__
1. Schedule an event.
2. Delete an event.
3. Display schedule.
4. Save schedule.
5. Load schedule.
6. Exit
Please enter a number between 1 - 6
1
Enter the event time:
Please enter a number between 0 - 23
2
Please enter a number between 0 - 59
22
Enter the event description:
descr1
Where in the array would you like to store this event
Please enter a number between 1 - 5
1
__= Scheduler v1.0 =__
1. Schedule an event.
2. Delete an event.
3. Display schedule.
4. Save schedule.
5. Load schedule.
6. Exit
Please enter a number between 1 - 6
1
Enter the event time:
Please enter a number between 0 - 23
3
Please enter a number between 0 - 59
33
Enter the event description:
descr2
Where in the array would you like to store this event
Please enter a number between 1 - 5
4
__= Scheduler v1.0 =__
1. Schedule an event.
2. Delete an event.
3. Display schedule.
4. Save schedule.
5. Load schedule.
6. Exit
Please enter a number between 1 - 6
12
Invalid Entry
Please enter a number between 1 - 6
6
Exiting Program
Index #[] Time Description
[1] 2:22 descr1
[4] 3:33 descr2
note the empty line in the final print, this is because the newline is part of the description, it must be remove is present in InputEvent
add a comment |
Why does my program keep printing garbage values?
You use non initialized data, in main you have :
Event e;
...
i = AddEventAtIndex(EventList, e, i);
where e is not initialized but used in AddEventAtIndex :
int AddEventAtIndex(Event list[], Event e, int i)
{
...
list[i].hour = e.hour;
list[i].minute = e.minute;
strcpy(list[i].description, e.description);
the behavior of strcpy with an non initialized value is undefined and can have a dramatic consequences.
Probably the call InputEvent( EventList );
must be replaced by InputEvent(&e);
In
for ( int j = 1 ; j < 6 ; j++)
printf("n[%d]nt%d:%d t %s", j, EventList[j].hour,
EventList[j].minute, EventList[j].description);
You access out of EventList when j is 5 and 6, do for instance :
for ( int j = 0 ; j < MAX ; j++)
printf("[%d]t%d:%d t %sn", j+1, EventList[j].hour,
EventList[j].minute, EventList[j].description);
But you also write entries never set printing 'garbage values', more the fact to print a string non initialized has undefined effect. A way to separate entries set and not set is to initialize all the hours by 24 and to test that value in the loop to write or not the entry.
I also changed the format because the newline after the index is not compatible with printf("Index #[]tTimetDescription");
and it is better to put the other newline after all rather than before to flush the output line, so also change printf("Index #[]tTimetDescription");
by puts("Index #[]tTimetDescription");
MAX is not a very good name because it is more a SUP, or just rename it by SIZE
In main you do not use eventListSize
In AddEventAtIndex
--i
must be done after i = InputRange(1, 5);
or just do i = InputRange(1, 5) - 1;
else when i values 5 list[i]
is out of list so you write out of list with an undefined behavior.
So finally the parameter i is useless, remove it.
The return index is also not used in main, you use it to assign i and do not used the value of i after. AddEventAtIndex do not need to return a value.
In InputRange
You do not check the result of scanf so the value of timenumber is undefined if a non valid integer was enter and if you do not purge the input so all the next scanf to get a number will not success
Also if the value is not in the range you just call InputRange(min, max);
without returning its value so you finally return the invalid value, put all in a for(;;)
for instance to only return when the value is correct.
You use fgets in InputEvent, to mix it with scanf to read number is a source of problem, replace the scanf by the use of fgets then a sscanf on the read line
In InputEvent
When you do fgetc(stdin);
you probably hope to bypass a newline but if the user enter characters after the number in InputRange (supposing a number was enter) fgets will read the first of them and fgets will not get the expected description. The solution is to do fgets then sscanf in InputRange as I said above allowing you to remove that fgetc
A proposal taking into account my remarks :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 5
struct event //data type called event which holds time and description for events
int hour; //holds the hour digit between 0-23
int minute; //holds the minute digit between 0-59
char description[41]; //holds the description for the reason of the alarm
;
typedef struct event Event; //allows coder to only have to use "Event" instead of "struct event"
int InputRange(int min, int max);
Event* InputEvent(Event *newEvent);
void AddEventAtIndex(Event list[], Event e);
//int InsertionSortEvent(Event list[], int *p_size, Event e);
//void DisplayEvent(Event e);
//void DisplayEventList(Event list[], int size);
//int DeleteEvent(Event list[], int i, int *p_size);
int main (void)
Event EventList[MAX];
Event e;
int choice;
/* mark uset entries */
for (int i = 0; i != MAX; ++i)
EventList[i].hour = 24;
do
printf("__= Scheduler v1.0 =__n");
printf("1. Schedule an event.n");
printf("2. Delete an event.n");
printf("3. Display schedule.n");
printf("4. Save schedule.n");
printf("5. Load schedule.n");
printf("6. Exitn");
switch(choice = InputRange(1, 6))
case 1:
InputEvent( &e );
AddEventAtIndex(EventList, e);
break;
/*case 2: pHead = deleteStudent(pHead);
break;
case 3: printf("Press 1 to search by ID or 2 to search by name: n");
scanf("%d", &search);
if (search == 1)
searchStudentID(pHead);
else if (search == 2)
searchStudentlName(pHead);
else
printf("Invalid selection");
break;
case 4: displayStudentInfo(pHead);
break;
case 5: saveStudentInfo(pHead);
break;
case 6: end(pHead);
break;*/
default: printf("Exiting Programnn");
while ( choice != 6 );
puts("Index #[]tTimetDescription");
for ( int j = 0 ; j < MAX ; j++)
if (EventList[j].hour != 24)
printf("t[%d]t%d:%d t %sn", j+1, EventList[j].hour,
EventList[j].minute, EventList[j].description);
int InputRange(int min, int max)
char line[32];
int timenumber;
for (;;)
printf("Please enter a number between %d - %dn", min, max);
if (fgets(line, sizeof(line), stdin) == NULL)
/* EOF */
exit(-1);
if ((sscanf(line, "%d", &timenumber) == 1) &&
(timenumber >= min) &&
(timenumber <= max))
return timenumber;
printf("Invalid Entryn");
Event* InputEvent(Event *newEvent)
if (newEvent != NULL) // quality assurance:
// make sure the pointer is valid
printf("Enter the event time:n");
newEvent->hour = InputRange(0, 23);
newEvent->minute = InputRange(0, 59);
printf("Enter the event description:n");
fgets(newEvent->description, sizeof(newEvent->description), stdin);
printf("n");
return newEvent;
void AddEventAtIndex(Event list[], Event e)
printf("Where in the array would you like to store this eventn");
int i = InputRange(1, 5) - 1;
list[i].hour = e.hour;
list[i].minute = e.minute;
strcpy(list[i].description, e.description);
Compilation and execution :
pi@raspberrypi:/tmp $ gcc -g -pedantic -Wextra -Wall c.c
pi@raspberrypi:/tmp $ ./a.out
__= Scheduler v1.0 =__
1. Schedule an event.
2. Delete an event.
3. Display schedule.
4. Save schedule.
5. Load schedule.
6. Exit
Please enter a number between 1 - 6
1
Enter the event time:
Please enter a number between 0 - 23
2
Please enter a number between 0 - 59
22
Enter the event description:
descr1
Where in the array would you like to store this event
Please enter a number between 1 - 5
1
__= Scheduler v1.0 =__
1. Schedule an event.
2. Delete an event.
3. Display schedule.
4. Save schedule.
5. Load schedule.
6. Exit
Please enter a number between 1 - 6
1
Enter the event time:
Please enter a number between 0 - 23
3
Please enter a number between 0 - 59
33
Enter the event description:
descr2
Where in the array would you like to store this event
Please enter a number between 1 - 5
4
__= Scheduler v1.0 =__
1. Schedule an event.
2. Delete an event.
3. Display schedule.
4. Save schedule.
5. Load schedule.
6. Exit
Please enter a number between 1 - 6
12
Invalid Entry
Please enter a number between 1 - 6
6
Exiting Program
Index #[] Time Description
[1] 2:22 descr1
[4] 3:33 descr2
note the empty line in the final print, this is because the newline is part of the description, it must be remove is present in InputEvent
add a comment |
Why does my program keep printing garbage values?
You use non initialized data, in main you have :
Event e;
...
i = AddEventAtIndex(EventList, e, i);
where e is not initialized but used in AddEventAtIndex :
int AddEventAtIndex(Event list[], Event e, int i)
{
...
list[i].hour = e.hour;
list[i].minute = e.minute;
strcpy(list[i].description, e.description);
the behavior of strcpy with an non initialized value is undefined and can have a dramatic consequences.
Probably the call InputEvent( EventList );
must be replaced by InputEvent(&e);
In
for ( int j = 1 ; j < 6 ; j++)
printf("n[%d]nt%d:%d t %s", j, EventList[j].hour,
EventList[j].minute, EventList[j].description);
You access out of EventList when j is 5 and 6, do for instance :
for ( int j = 0 ; j < MAX ; j++)
printf("[%d]t%d:%d t %sn", j+1, EventList[j].hour,
EventList[j].minute, EventList[j].description);
But you also write entries never set printing 'garbage values', more the fact to print a string non initialized has undefined effect. A way to separate entries set and not set is to initialize all the hours by 24 and to test that value in the loop to write or not the entry.
I also changed the format because the newline after the index is not compatible with printf("Index #[]tTimetDescription");
and it is better to put the other newline after all rather than before to flush the output line, so also change printf("Index #[]tTimetDescription");
by puts("Index #[]tTimetDescription");
MAX is not a very good name because it is more a SUP, or just rename it by SIZE
In main you do not use eventListSize
In AddEventAtIndex
--i
must be done after i = InputRange(1, 5);
or just do i = InputRange(1, 5) - 1;
else when i values 5 list[i]
is out of list so you write out of list with an undefined behavior.
So finally the parameter i is useless, remove it.
The return index is also not used in main, you use it to assign i and do not used the value of i after. AddEventAtIndex do not need to return a value.
In InputRange
You do not check the result of scanf so the value of timenumber is undefined if a non valid integer was enter and if you do not purge the input so all the next scanf to get a number will not success
Also if the value is not in the range you just call InputRange(min, max);
without returning its value so you finally return the invalid value, put all in a for(;;)
for instance to only return when the value is correct.
You use fgets in InputEvent, to mix it with scanf to read number is a source of problem, replace the scanf by the use of fgets then a sscanf on the read line
In InputEvent
When you do fgetc(stdin);
you probably hope to bypass a newline but if the user enter characters after the number in InputRange (supposing a number was enter) fgets will read the first of them and fgets will not get the expected description. The solution is to do fgets then sscanf in InputRange as I said above allowing you to remove that fgetc
A proposal taking into account my remarks :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 5
struct event //data type called event which holds time and description for events
int hour; //holds the hour digit between 0-23
int minute; //holds the minute digit between 0-59
char description[41]; //holds the description for the reason of the alarm
;
typedef struct event Event; //allows coder to only have to use "Event" instead of "struct event"
int InputRange(int min, int max);
Event* InputEvent(Event *newEvent);
void AddEventAtIndex(Event list[], Event e);
//int InsertionSortEvent(Event list[], int *p_size, Event e);
//void DisplayEvent(Event e);
//void DisplayEventList(Event list[], int size);
//int DeleteEvent(Event list[], int i, int *p_size);
int main (void)
Event EventList[MAX];
Event e;
int choice;
/* mark uset entries */
for (int i = 0; i != MAX; ++i)
EventList[i].hour = 24;
do
printf("__= Scheduler v1.0 =__n");
printf("1. Schedule an event.n");
printf("2. Delete an event.n");
printf("3. Display schedule.n");
printf("4. Save schedule.n");
printf("5. Load schedule.n");
printf("6. Exitn");
switch(choice = InputRange(1, 6))
case 1:
InputEvent( &e );
AddEventAtIndex(EventList, e);
break;
/*case 2: pHead = deleteStudent(pHead);
break;
case 3: printf("Press 1 to search by ID or 2 to search by name: n");
scanf("%d", &search);
if (search == 1)
searchStudentID(pHead);
else if (search == 2)
searchStudentlName(pHead);
else
printf("Invalid selection");
break;
case 4: displayStudentInfo(pHead);
break;
case 5: saveStudentInfo(pHead);
break;
case 6: end(pHead);
break;*/
default: printf("Exiting Programnn");
while ( choice != 6 );
puts("Index #[]tTimetDescription");
for ( int j = 0 ; j < MAX ; j++)
if (EventList[j].hour != 24)
printf("t[%d]t%d:%d t %sn", j+1, EventList[j].hour,
EventList[j].minute, EventList[j].description);
int InputRange(int min, int max)
char line[32];
int timenumber;
for (;;)
printf("Please enter a number between %d - %dn", min, max);
if (fgets(line, sizeof(line), stdin) == NULL)
/* EOF */
exit(-1);
if ((sscanf(line, "%d", &timenumber) == 1) &&
(timenumber >= min) &&
(timenumber <= max))
return timenumber;
printf("Invalid Entryn");
Event* InputEvent(Event *newEvent)
if (newEvent != NULL) // quality assurance:
// make sure the pointer is valid
printf("Enter the event time:n");
newEvent->hour = InputRange(0, 23);
newEvent->minute = InputRange(0, 59);
printf("Enter the event description:n");
fgets(newEvent->description, sizeof(newEvent->description), stdin);
printf("n");
return newEvent;
void AddEventAtIndex(Event list[], Event e)
printf("Where in the array would you like to store this eventn");
int i = InputRange(1, 5) - 1;
list[i].hour = e.hour;
list[i].minute = e.minute;
strcpy(list[i].description, e.description);
Compilation and execution :
pi@raspberrypi:/tmp $ gcc -g -pedantic -Wextra -Wall c.c
pi@raspberrypi:/tmp $ ./a.out
__= Scheduler v1.0 =__
1. Schedule an event.
2. Delete an event.
3. Display schedule.
4. Save schedule.
5. Load schedule.
6. Exit
Please enter a number between 1 - 6
1
Enter the event time:
Please enter a number between 0 - 23
2
Please enter a number between 0 - 59
22
Enter the event description:
descr1
Where in the array would you like to store this event
Please enter a number between 1 - 5
1
__= Scheduler v1.0 =__
1. Schedule an event.
2. Delete an event.
3. Display schedule.
4. Save schedule.
5. Load schedule.
6. Exit
Please enter a number between 1 - 6
1
Enter the event time:
Please enter a number between 0 - 23
3
Please enter a number between 0 - 59
33
Enter the event description:
descr2
Where in the array would you like to store this event
Please enter a number between 1 - 5
4
__= Scheduler v1.0 =__
1. Schedule an event.
2. Delete an event.
3. Display schedule.
4. Save schedule.
5. Load schedule.
6. Exit
Please enter a number between 1 - 6
12
Invalid Entry
Please enter a number between 1 - 6
6
Exiting Program
Index #[] Time Description
[1] 2:22 descr1
[4] 3:33 descr2
note the empty line in the final print, this is because the newline is part of the description, it must be remove is present in InputEvent
Why does my program keep printing garbage values?
You use non initialized data, in main you have :
Event e;
...
i = AddEventAtIndex(EventList, e, i);
where e is not initialized but used in AddEventAtIndex :
int AddEventAtIndex(Event list[], Event e, int i)
{
...
list[i].hour = e.hour;
list[i].minute = e.minute;
strcpy(list[i].description, e.description);
the behavior of strcpy with an non initialized value is undefined and can have a dramatic consequences.
Probably the call InputEvent( EventList );
must be replaced by InputEvent(&e);
In
for ( int j = 1 ; j < 6 ; j++)
printf("n[%d]nt%d:%d t %s", j, EventList[j].hour,
EventList[j].minute, EventList[j].description);
You access out of EventList when j is 5 and 6, do for instance :
for ( int j = 0 ; j < MAX ; j++)
printf("[%d]t%d:%d t %sn", j+1, EventList[j].hour,
EventList[j].minute, EventList[j].description);
But you also write entries never set printing 'garbage values', more the fact to print a string non initialized has undefined effect. A way to separate entries set and not set is to initialize all the hours by 24 and to test that value in the loop to write or not the entry.
I also changed the format because the newline after the index is not compatible with printf("Index #[]tTimetDescription");
and it is better to put the other newline after all rather than before to flush the output line, so also change printf("Index #[]tTimetDescription");
by puts("Index #[]tTimetDescription");
MAX is not a very good name because it is more a SUP, or just rename it by SIZE
In main you do not use eventListSize
In AddEventAtIndex
--i
must be done after i = InputRange(1, 5);
or just do i = InputRange(1, 5) - 1;
else when i values 5 list[i]
is out of list so you write out of list with an undefined behavior.
So finally the parameter i is useless, remove it.
The return index is also not used in main, you use it to assign i and do not used the value of i after. AddEventAtIndex do not need to return a value.
In InputRange
You do not check the result of scanf so the value of timenumber is undefined if a non valid integer was enter and if you do not purge the input so all the next scanf to get a number will not success
Also if the value is not in the range you just call InputRange(min, max);
without returning its value so you finally return the invalid value, put all in a for(;;)
for instance to only return when the value is correct.
You use fgets in InputEvent, to mix it with scanf to read number is a source of problem, replace the scanf by the use of fgets then a sscanf on the read line
In InputEvent
When you do fgetc(stdin);
you probably hope to bypass a newline but if the user enter characters after the number in InputRange (supposing a number was enter) fgets will read the first of them and fgets will not get the expected description. The solution is to do fgets then sscanf in InputRange as I said above allowing you to remove that fgetc
A proposal taking into account my remarks :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 5
struct event //data type called event which holds time and description for events
int hour; //holds the hour digit between 0-23
int minute; //holds the minute digit between 0-59
char description[41]; //holds the description for the reason of the alarm
;
typedef struct event Event; //allows coder to only have to use "Event" instead of "struct event"
int InputRange(int min, int max);
Event* InputEvent(Event *newEvent);
void AddEventAtIndex(Event list[], Event e);
//int InsertionSortEvent(Event list[], int *p_size, Event e);
//void DisplayEvent(Event e);
//void DisplayEventList(Event list[], int size);
//int DeleteEvent(Event list[], int i, int *p_size);
int main (void)
Event EventList[MAX];
Event e;
int choice;
/* mark uset entries */
for (int i = 0; i != MAX; ++i)
EventList[i].hour = 24;
do
printf("__= Scheduler v1.0 =__n");
printf("1. Schedule an event.n");
printf("2. Delete an event.n");
printf("3. Display schedule.n");
printf("4. Save schedule.n");
printf("5. Load schedule.n");
printf("6. Exitn");
switch(choice = InputRange(1, 6))
case 1:
InputEvent( &e );
AddEventAtIndex(EventList, e);
break;
/*case 2: pHead = deleteStudent(pHead);
break;
case 3: printf("Press 1 to search by ID or 2 to search by name: n");
scanf("%d", &search);
if (search == 1)
searchStudentID(pHead);
else if (search == 2)
searchStudentlName(pHead);
else
printf("Invalid selection");
break;
case 4: displayStudentInfo(pHead);
break;
case 5: saveStudentInfo(pHead);
break;
case 6: end(pHead);
break;*/
default: printf("Exiting Programnn");
while ( choice != 6 );
puts("Index #[]tTimetDescription");
for ( int j = 0 ; j < MAX ; j++)
if (EventList[j].hour != 24)
printf("t[%d]t%d:%d t %sn", j+1, EventList[j].hour,
EventList[j].minute, EventList[j].description);
int InputRange(int min, int max)
char line[32];
int timenumber;
for (;;)
printf("Please enter a number between %d - %dn", min, max);
if (fgets(line, sizeof(line), stdin) == NULL)
/* EOF */
exit(-1);
if ((sscanf(line, "%d", &timenumber) == 1) &&
(timenumber >= min) &&
(timenumber <= max))
return timenumber;
printf("Invalid Entryn");
Event* InputEvent(Event *newEvent)
if (newEvent != NULL) // quality assurance:
// make sure the pointer is valid
printf("Enter the event time:n");
newEvent->hour = InputRange(0, 23);
newEvent->minute = InputRange(0, 59);
printf("Enter the event description:n");
fgets(newEvent->description, sizeof(newEvent->description), stdin);
printf("n");
return newEvent;
void AddEventAtIndex(Event list[], Event e)
printf("Where in the array would you like to store this eventn");
int i = InputRange(1, 5) - 1;
list[i].hour = e.hour;
list[i].minute = e.minute;
strcpy(list[i].description, e.description);
Compilation and execution :
pi@raspberrypi:/tmp $ gcc -g -pedantic -Wextra -Wall c.c
pi@raspberrypi:/tmp $ ./a.out
__= Scheduler v1.0 =__
1. Schedule an event.
2. Delete an event.
3. Display schedule.
4. Save schedule.
5. Load schedule.
6. Exit
Please enter a number between 1 - 6
1
Enter the event time:
Please enter a number between 0 - 23
2
Please enter a number between 0 - 59
22
Enter the event description:
descr1
Where in the array would you like to store this event
Please enter a number between 1 - 5
1
__= Scheduler v1.0 =__
1. Schedule an event.
2. Delete an event.
3. Display schedule.
4. Save schedule.
5. Load schedule.
6. Exit
Please enter a number between 1 - 6
1
Enter the event time:
Please enter a number between 0 - 23
3
Please enter a number between 0 - 59
33
Enter the event description:
descr2
Where in the array would you like to store this event
Please enter a number between 1 - 5
4
__= Scheduler v1.0 =__
1. Schedule an event.
2. Delete an event.
3. Display schedule.
4. Save schedule.
5. Load schedule.
6. Exit
Please enter a number between 1 - 6
12
Invalid Entry
Please enter a number between 1 - 6
6
Exiting Program
Index #[] Time Description
[1] 2:22 descr1
[4] 3:33 descr2
note the empty line in the final print, this is because the newline is part of the description, it must be remove is present in InputEvent
edited Mar 26 at 23:28
answered Mar 26 at 22:01
brunobruno
19.3k3 gold badges16 silver badges28 bronze badges
19.3k3 gold badges16 silver badges28 bronze badges
add a comment |
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55366566%2fwhy-does-my-program-keep-printing-garbage-values%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Provided code does not compile. Start by formatting the code with proper indents, fixing compile errors, and then recompiling with the maximum warning level and correcting warnings produced by the compiler.
– Reticulated Spline
Mar 26 at 21:43
You have several problems in your program, see my answer listing them more a proposal to solve them and an example of execution
– bruno
Mar 26 at 23:10