All I want to do is scanf and printf

but it aint working.

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.

Thanks
Last edited on
Why are you using scanf and printf?
http://www.gidnetwork.com/b-59.html

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.

On how to use scanf() and printf() correctly:
http://www.cplusplus.com/reference/clibrary/cstdio/scanf/
http://www.cplusplus.com/reference/clibrary/cstdio/printf/
Last edited on
this also segment faults:

1
2
3
   char ch;
   printf("type something \n");
   scanf("%c", ch);

as soon as i hit return it barfs.
Last edited on
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.
Last edited on
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()
its automatically 500K big


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?


It's annoying, just use std::getline().
by including <iostream> the filesize is automatically at least 477K.
by just including <stdio.h> the filesize is only at least 15.3K
Last edited on
What platform and compiler? I just checked on a current project I'm working on, and with the following includes:

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdlib.h>
#include <crtdbg.h> 
#include <tchar.h>
#include <iostream>
#include <iomanip>
#include <time.h>
#include <string>
#include <fstream>
#include <vector>
#include <list>
#include <sstream>
#include <windows.h> 


My debug executable filesize is 306k. That's with over 5000 lines of my own code. Visual Studio 2008.

Besides, what environment are you working in where 450k is a substantial difference in filesize?
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.

http://www.cplusplus.com/forum/windows/3430/
Last edited on
I'm using the mingw compiler with codeblocks on windows xp. Should that make a difference?
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.

Hope this helps.
#include<stdio.h>
#include<string>

char name[10] = "";

printf("\nEnter name: ");
scanf("%s", &name);
Last edited on
@k2r: Uh...no.
my bad...I was wrong...

answer removed.
Last edited on
Uh, yes it does. That's exactly what the "%s" scanf() format specifier does...

BTW, you should never do that. Same danger as using gets()...
Clarification:
I was saying "no" the using %s without a number like %9s.
Topic archived. No new replies allowed.