| | |
Help with dates
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
Greetings phr0stbyt3,
Getting the data is not difficult at all. We can either use scanf() or fgets().
scanf()
int scanf(const char *format [, argument, ...]);
> Reads data from the standard input (stdin) and stores it into the locations given by argument(s). Locations pointed by each argument are filled with their corresponding type of value requested in the format string.
In this case, we could make a simple program as the following: The way this works is simple. We call scanf() to get any input from the input stream. Once we get the stream we use scanf() to pull out what we want. For instance if we had: That means if we entered in "Hello there world" s1 would be Hello, s2 would be there and s3 would be world.
* scanf() note: These arguments must be pointers: if you want to store the result of a scanf operation on a standard variable you should precede it with the reference operator, i.e. an ampersand sign (&). It is not neccessary to add the (&) symbol when working with character arrays since they respond similiarly to pointers.
fgets()
char *fgets (char *string, int num, FILE *stream);
> Reads characters from stream and stores them in string until (num -1) characters have been read or a newline or EOF character is reached, whichever comes first.
fgets() is used mostly when receiving a buffer from the stream. Because so far we don't know what is coming, and scanf() could overflow. How? scanf() has no limitation of how much we try to read, and forcing the function to read could cause issues. Though in fact scanf() returns the number of items succesfully read. So in that case we could do: We could get away with using scanf(). It's not recommended to use often though there are the exceptions. Back on topic, fgets() is safter to use because we get to set how much of our string the stream sends to. For instance, if we used scanf() and had a input size of 100 and our local variable is only a size of 50 there could be a problem. fgets() relieves us of this. In this case we could do: This saves us from overflow. As you may have seen, we used stdin as our FILE stream. That's fine, since the stream stdin is our input from the console window. Even if our input was 2000 bytes, fgets() only sends the first 24 bytes to our character array str.
On the other hand
Though, if you were to use scanf(), you could get away with it since you are dealing with integer values only. For example: This will make sure our scanf() is complete with three entries and so forth.
I hope this helps. If you have further questions, please feel free to ask.
- Stack Overflow
Getting the data is not difficult at all. We can either use scanf() or fgets().
scanf()
int scanf(const char *format [, argument, ...]);
> Reads data from the standard input (stdin) and stores it into the locations given by argument(s). Locations pointed by each argument are filled with their corresponding type of value requested in the format string.
In this case, we could make a simple program as the following:
#include <stdio.h> int main() { int a; printf("Enter a number: "); scanf("%d", &a); printf("You entered %d\n", a); return 0; }
C Syntax (Toggle Plain Text)
scanf("%s %s %s", s1, s2, s3);
* scanf() note: These arguments must be pointers: if you want to store the result of a scanf operation on a standard variable you should precede it with the reference operator, i.e. an ampersand sign (&). It is not neccessary to add the (&) symbol when working with character arrays since they respond similiarly to pointers.
fgets()
char *fgets (char *string, int num, FILE *stream);
> Reads characters from stream and stores them in string until (num -1) characters have been read or a newline or EOF character is reached, whichever comes first.
fgets() is used mostly when receiving a buffer from the stream. Because so far we don't know what is coming, and scanf() could overflow. How? scanf() has no limitation of how much we try to read, and forcing the function to read could cause issues. Though in fact scanf() returns the number of items succesfully read. So in that case we could do:
if (scanf("%s %d", s1, &i) == 2) { // ... }
#include <stdio.h> int main() { char str[25]; fgets(str, 24, stdin); printf("Input stream: %s", str); return 0; }
On the other hand
Though, if you were to use scanf(), you could get away with it since you are dealing with integer values only. For example:
int month, day, year; if (scanf("%d/%d/%d", &month, &day, &year) == 3) { // ... }
I hope this helps. If you have further questions, please feel free to ask.
- Stack Overflow
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.
IRC
Channel: irc.daniweb.com
Room: #c, #shell
IRC
Channel: irc.daniweb.com
Room: #c, #shell
•
•
Join Date: Oct 2004
Posts: 16
Reputation:
Solved Threads: 1
ok i thought it made sense, but now that im trying to work with it in dev c++ its actin up... what i want to do is read 12/25/2004 etc and store 12, 25, and 2004 in main in int's... we havent really covered the stdio.h in my class yet. this is what i have so far:
i dont think we can use streams or arrays. also, when i run this program it performs an illegal operation after i put in "12/12/2004" i need something a little less advanced
C Syntax (Toggle Plain Text)
#include <iostream> #include <stdio.h> using namespace std; void getData(int&, int&, int&); int main() { int month; int day; int year; getData(month, day, year); cout << "\n" << month << "\n" << day << "\n" << year; system("pause"); return 0; } void getData(int& month, int& day, int& year) { cout << "Enter your birthday in the form of: mm/dd/yyyy"; scanf("%s %s %s", month, day, year); return; }
i dont think we can use streams or arrays. also, when i run this program it performs an illegal operation after i put in "12/12/2004" i need something a little less advanced
Alright,
Here are the changes: Change 1
We changed
void getData(int&, int&, int&);
To:
void getData(int *, int *, int *);
Why?
> Because we are passing the addresses variable to our function getData().
Change 2
getData(&month, &day, &year);
> This was changed because this is actually passing the variables memory address. Hence the reason we used the unary dereferencing operator '*' in our function declaration
Change 3
void getData(int *month, int *day, int *year) {
> We changed this because we changed our first declaration above.
Change 4
int m, d, y;
> We need some local variables if we intend to change the variables from within the function.
Change 5
if (scanf("%d/%d/%d", &m, &d, &y) == 3) {
> Just to ensure that we got all 3 variables. A previous problem was that you used %s instead of %d or %i. The %s goes for strings, %d for doubles and %i for integers.
Change 6
*month = m;
*day = d;
*year = y;
> Remember how we passed our address? Well now we need to send the new data to our variable. The only thing is our data can only be accessed if we use the '*' operator. We can't set the data to the address, we have to set the data to the data.
Hope this helps,
- Stack Overflow
Here are the changes:
#include <iostream> #include <stdio.h> using namespace std; void getData(int *, int *, int *); int main() { int month; int day; int year; getData(&month, &day, &year); cout << "Month: " << month << endl << "Day: " << day << endl << "Year: " << year << endl; system("pause"); return 0; } void getData(int *month, int *day, int *year) { int m, d, y; cout << "Enter your birthday in the form of: mm/dd/yyyy" << endl; if (scanf("%d/%d/%d", &m, &d, &y) == 3) { *month = m; *day = d; *year = y; } return; }
We changed
void getData(int&, int&, int&);
To:
void getData(int *, int *, int *);
Why?
> Because we are passing the addresses variable to our function getData().
Change 2
getData(&month, &day, &year);
> This was changed because this is actually passing the variables memory address. Hence the reason we used the unary dereferencing operator '*' in our function declaration
Change 3
void getData(int *month, int *day, int *year) {
> We changed this because we changed our first declaration above.
Change 4
int m, d, y;
> We need some local variables if we intend to change the variables from within the function.
Change 5
if (scanf("%d/%d/%d", &m, &d, &y) == 3) {
> Just to ensure that we got all 3 variables. A previous problem was that you used %s instead of %d or %i. The %s goes for strings, %d for doubles and %i for integers.
Change 6
*month = m;
*day = d;
*year = y;
> Remember how we passed our address? Well now we need to send the new data to our variable. The only thing is our data can only be accessed if we use the '*' operator. We can't set the data to the address, we have to set the data to the data.
Hope this helps,
- Stack Overflow
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.
IRC
Channel: irc.daniweb.com
Room: #c, #shell
IRC
Channel: irc.daniweb.com
Room: #c, #shell
![]() |
Similar Threads
- VB/Access - Difference between two dates (Visual Basic 4 / 5 / 6)
- Script to compare dates? (Shell Scripting)
- help with PHP script to return dates (PHP)
- help needed with two methods - Dates (Java)
- Comparing and sorting of Dates. (C++)
- dates (Visual Basic 4 / 5 / 6)
Other Threads in the C Forum
- Previous Thread: -$- Estimated Amount Of Money -$-
- Next Thread: piglatin program with fgets, strtok, and strlen
| Thread Tools | Search this Thread |
#include adobe api array arrays asterisks binarysearch calculate char cm copyanyfile copyimagefile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush fgets file fork forloop framework frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o incrementoperators initialization kernel kilometer km linked linkedlist linux linuxsegmentationfault list lists locate logical_drives loopinsideloop. match matrix meter microsoft motherboard mqqueue multi mysql number odf open opensource owf pattern pdf performance pointer pointers posix probleminc process program programming pyramidusingturboccodes read recursion recv repetition research scanf scheduling scripting segmentationfault send shape socket socketprograming stack standard string strings systemcall testautomation unix user voidmain() wab win32api windows.h





