Click to See Complete Forum and Search --> : Help!


mhuneidi
April 16th, 2008, 10:53 PM
So I'm supposed to create a program that lists all the dates from starting date to ending date that is input by the user. I got the program to compile.. But when I open the .txt file that is supposed to contain the output it is empty... and I can't really figure out where I went wrong... Can someone please help me out?!
Thanks!



#include <stdio.h>

int main()
{

FILE *test;
int day, startingDay, endingDay;
int year, startingYear, endingYear;
int month, startingMonth, endingMonth;//can save the months as numbers, jan=1 etc..
int x=0;

test = fopen ("test.txt", "w");

printf("Please enter the StartingDate in the form MM/DD/YYYY\n");
scanf("%d/%d/%d", &startingMonth, &startingDay, &startingYear);

printf("Please enter the EndingDate in the form MM/DD/YYYY\n");
scanf("%d/%d/%d", &endingMonth, &endingDay, &endingYear);

//ask user for starting date and ending date
//save as startingYear, endingYear, startingDay, endingDay, etc...


for (year = startingYear; year < endingYear; year++)
{

for (month = startingMonth; month < 12; month++)
{

for (day = startingDay; day <= x; day++)
{


switch(month)
{
case 1:
x=31;
fprintf(test, "January %d, %d",day, year);


case 2:
x=28;
fprintf(test, "February %d, %d",day, year);


case 3:
x=31;
fprintf(test, "March %d, %d",day, year);


case 4:
x=30;
fprintf(test, "April %d, %d",day, year);


case 5:
x=31;
fprintf(test, "May %d, %d",day, year);


case 6:
x=30;
fprintf(test, "June %d, %d",day, year);


case 7:
x=31;
fprintf(test, "July %d, %d",day, year);


case 8:
x=31;
fprintf(test, "August %d, %d",day, year);


case 9:
x=30;
fprintf(test, "September %d, %d",day, year);


case 10:
x=31;
fprintf(test, "October %d, %d",day, year);


case 11:
x=30;
fprintf(test, "November %d, %d", day, year);

case 12:
x=31;
fprintf(test, "December %d, %d", day, year);

}


if ((month = endingMonth) && (day = endingDay) && (year = endingYear)){
break;//exit all loops and finish program
}
}
startingDay = 0;
}
startingMonth = 0;
}

fclose(test);
return 0;
}

aosmith
April 16th, 2008, 11:23 PM
why not just do it with an fstream?

#include <fstream>

...

ofstream outfile;
outfile.open("name.txt");

Also if you're running vista you must manually locate the app in the debug dir and run in admin mode for any file interaction to work properly.

mhuneidi
April 16th, 2008, 11:52 PM
why not just do it with an fstream?

#include <fstream>

...

ofstream outfile;
outfile.open("name.txt");

Also if you're running vista you must manually locate the app in the debug dir and run in admin mode for any file interaction to work properly.

I get the error:
P666.c:2: fstream: No such file or directory
... and im not sure how to locate the app in the debugger directory ...