A common task in C or C++ is to get a string from somewhere and convert it to an integer. "Use `atoi`" is a common suggestion from the unwashed masses, and on the surface it works like a charm: #include <stdio.h> #include <stdlib.h> long long square(int value) { return value * value; } long long cube(int value) { return value * square(value); } int main(void) { char buf[BUFSIZ]; fputs("Enter an integer: ", stdout); fflush(stdout); if (fgets(buf, sizeof buf, stdin) != NULL) { int value = atoi(buf); printf("%d\t%lld\t%lld\n", value, square(value), cube(value)); } return 0; } "So what's the problem?", you might …

Member Avatar
Member Avatar
+6 forum 5

Hi! I have to write the programm, which will receive any number (at sec) and calculate it to weeks, days, hours, minutes and sec; for e.x. 4102 sec ---> 1h 8m and 22s The problem is that it's forbidden to use atoi(), scanf() and simmilar functions. Insead of them, I should use getchar(), putchar(), printf(). So, I start with a converting string to int. [CODE=c] #include <stdio.h> #include <stdlib.h> /* MAIN */ int main() { int number; int l; while ((number = getchar()) != EOF) { l = cislo - '0'; putchar(l); } return 0; } [/CODE] It doesn't work …

Member Avatar
Member Avatar
+0 forum 2

I am writing a program that writes log entries to an external text file and returns them upon request. When called, the return function should output the log entries between two user- submitted dates or outputs the last 100kb of the file if that's what's requested. The logfile stores each entry like: [ICODE]YYYY-MM-DD Logmessage[/ICODE] But I can't get my program to compare only dates. It compares each string it grabs, and when it compares text to a date, it doens't behave properly. Here's the code: [CODE]int returntouser() { std::stringstream returnstr; std::string begdate, enddate; int validdate = 0; while (validdate == …

Member Avatar
Member Avatar
+0 forum 1

The End.