I'm trying to change all the cins and couts from my program to scanfs and printfs to make the program size a little smaller. but i keep getting these errors. I've never really used printf and scanf before.
1 2 3
int num;
scanf("%d", num);
printf("\n you typed: ",num, "\n");
If I type 6, or f, the output is the same: "you typed: "
This also doesn't work:
1 2
string str;
scanf("%s", str);
Please will someone tell me how to make scanf and printf work with strings. I mean the class string not the char pointer string.
Printf isn't as bad, but it's still kind of broken, you can pass it invalid data and it will just read it happily...and I doubt it would help your program size much, if at all.
Anyway, you can't make them work with std::strings, because std::strings are from C++, not C.
firedraco well because of the fact that you create an empty c++ program its automaticallay 500K big, compare that to an empty c program which is like 15k.
Follow up question:
If not to use scanf what should I use? (besides cin)
How can I use scanf with character arrays to accept an anysized string dynamically? I thought about something like this:
char ch;
string str;
do{
scanf("%c", ch);
str += ch;
}while (ch != 13); //13 is the ascii code for return right???
int size = str.length();
char charray[size];
But being that I can't even get it to work right to read a single char I can't even begin to test to see if it works.
Read the links about printf() and scanf() that firedraco posted, as they tell you exactly what to pass as parameters. Also, your mixture of C/C++ is not a good idea. I would suggest sticking to one or the other.
Your snippet seems to be trying to read char's into a string (C++) via scanf (C) which I can't say is something you should do when you have the C++ function getline()
What are you linking? Both C and C++ should be the same size if you just have this:
1 2 3
int main() {
return 0;
}
Use std::getline() with std::cin, it not only lets you avoid using the not very good C functions, but it lets you use the much better things like std::string, etc. AFAIK there is no way to get input besides std::cin and scanf() (except maybe getchar(), but that probably uses one or the other).
How can I use scanf with character arrays to accept an any sized string dynamically?
firedraco well because of the fact that you create an empty c++ program its automaticallay 500K big, compare that to an empty c program which is like 15k.
Oh, for the love of God. Is this 1995? Maybe you should try machine language if you're so worried about binary size that you can't spare a few hundred Ks.
Modern versions of the STL are sufficiently optimized that you don't need to resort to dangerous hacks to make it work. The GCC's STL is very good. Stick with cin and cout. You get type safety, ease of use, and extensibility. With scanf() and printf(), you get an unexplained knife in the back when you least expect it.
When you compile your release version, make sure to link against the release versions of the libraries, and strip all debug symbols from the final executable.