-
need help..quickly
A program that will accept user input:
a the day of the wk (eg tuesday)
b an 'n' number of days from now, between 1000 and 10000
The program should output the day of the wk after 'n' days
input: monday output: thursday
BUT....
really simple
NO
- functions
- loops
only IF's and strings and the % (mod) operator
to compare use strcmp(string1, string2)
-
Can you make an attempt at getting this problem started and then post your efforts here?
-
I kinda know what the program needs...I just can't seem to get the ifs and math into it
<code>
int n;
char days[]="monday", "tuesday"...etc.
char day[];
char d[];
printf("Enter the day today: ");
scanf( );
printf("Enter the number between 1000 and 10000: ");
scanf( );
strcmp(days, day);
amt = n % 7
if (
?
?
?
?
printf("The day will be%s\n",d);
</code>
thats all I got
-
I'd appreciate any help at all...Im a beginner at this and Im making it harder than it has to be, it doesn't help with deadlines.
-
Daisy
Ignour people that are rude to you thinking you want to rely only on other people to do your work for you. You need people to help you then ask. If I was good enough I would write it out for you. I failed a course just cause no one would help on one solution. It was only a part time evening course as well, not like it woudl ever get me anything.
-
Thank you. I don't want to rely on anyone. And since I did write out what I kinda thought the code would be no one has helped anyways. Im in college for computer programming/analyst as a mature student. What about you?
-
how about something on the lines of this
correct me if im wrong, just giving it a shot.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *days[] = { "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday" };
int num;
int newday;
char day[10];
printf( "Enter day of week and number of days between 1000-10000:\n" );
scanf( "%s %d", &day, &num );
if ( num < 1000 || num > 10000 ) {
printf( "Invalid number of days\n" );
exit(EXIT_SUCCESS);
}
newday=num%7;
printf( "newday: %d\n", newday );
if ( strcmp( day, days[0] ) == 0 ) {
printf( "output: %s\n", days[newday] );
}
else if ( strcmp( day, days[1] ) == 0 ) {
printf( "output: %s\n", days[newday] );
}
else if ( strcmp( day, days[2] ) == 0 ) {
printf( "output: %s\n", days[newday] );
}
else if ( strcmp( day, days[3] ) == 0 ) {
printf( "output: %s\n", days[newday] );
}
else if ( strcmp( day, days[4] ) == 0 ) {
printf( "output: %s\n", days[newday] );
}
else if ( strcmp( day, days[5] ) == 0 ) {
printf( "output: %s\n", days[newday] );
}
else if ( strcmp( day, days[6] ) == 0 ) {
printf( "output: %s\n", days[newday] );
}
else {
printf( "not valid input day\n" );
exit(EXIT_SUCCESS);
}
return 0;
}
-
check if output is correct
added newday to check the value of the mod
so delete it once u finish checking i guess.
HOPE THAT HELPS
its probably not perfect but you can fix up the bugs or the awkward answers.
might be mod 6 cause i started at 0
but i think u can edit the *days[] and throw in some crap day at the zero position and just start at days[1]
-
these are the variables (simple):
int dayasint;
int remainderdays;
int numberofdaysfromnow;
char day[10];
from the code above from kurz7 I got it to work but anytime it's 0 it comes out monday, 1 comes out tuesday ... no matter what day you input.
do you say:
if (strcmp(day,"monday")
{
monday = 0; tuesday = 1; wednesday = 2;
//etc...through all the if-days???
And how would you write that?
-
A Step by step solution -- for learning, I'd recommend starting from scratch but using kurz7's code for a few key features:
1) define the day-of-week array
2) input the day-of-week
3) using if statements, compare the input with the seven values in the array (here of course a loop would be VERY useful) Save the index in 'dow'
4) input your number
5) add 'dow' to it
6) take the modulo-7 (%) of the value -- this value will be the day index for the new day
7) output from the day-of-week array
That should do it.
Walt
-
first of all....
no loops
no weekday string only:
int dayasint;
I've already used kurz7 program and it has no errors but doesn't work properly with the weekday string.
this is the first assignment for the year. The teacher hasn't gone through much of anything other than ifs, and strings. and I have a list of the variables the teacher used...above.
-
okay got the code, teacher helped, but when I input any day and number it still comes out...The day is a Monday...all the time. I traced through it and it comes to the first IF and when it outputs Monday it goes straight to the end.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int intDay;
int num;
int newday;
char day[10];
clrscr();
printf("Enter a day of the week and number of days between 1000 and 10000:\n");
scanf("%s%d",&day,&num);
newday = num % 7;
printf("newday: %d\n",newday);
if (strcmp(day,"monday"))
{
intday = 0;
printf("The day is a Monday");
}
else if (strcmp(day,"tuesday"))
{
intday = 1;
printf("The day is a Tuesday");
}
else if (strcmp(day,"wednesday"))
{
intday = 2;
printf("The day is a Wednesday");
}
else if (strcmp(day,"thursday"))
{
intday = 3;
printf("The day is a Thursday");
}
else if (strcmp(day,"friday"))
{
intday = 4;
printf("The day is a Friday");
}
else if (strcmp(day,"saturday"))
{
intday = 5;
printf("The day is a Saturday");
}
else if (strcmp(day,"sunday"))
{
intday = 6;
printf("The day is a Sunday");
}
else
{
printf("Incorrect Input");
}
return 0;
}
-
Quote:
Originally posted by digy
Code:
scanf("%s%d", day, &num);
try using stricmp
if(stricmp(day, "monday") == 0)
puts("use fgets for strings");
I didn't run your code, so there might be something causing the default behavior.
HTH
-
It looks to me that your scanf is not working.
scanf("%s%d",&day,&num);
for me resulted in junk in both day and num.
Try adding a printf after the scanf to see what is being entered:
printf("day=[%s] num=[%d] \n", day, num);
-
nope all is working....I put checkups in to see what the computer says I inputted. I still need help on this.
remember:
very simple (no loops, functions)
the code I am working with is the code I wrote a few replys up.
except for a change......
if (strcmp(day,"monday")==0)
intday = 1;
that's all folks....
can't figure out what to do with intday = # and to output the answer....