C++ Question

jmcoreymv

Diamond Member
Oct 9, 1999
4,264
0
0
How do you use printf to print a variable? I get a const char error when I try to do it.
 

const char* str = "Hello World.";
printf("%s\n", str);

What's the problem?

 

FatAlbo

Golden Member
May 11, 2000
1,423
0
0
I think printf() is more C related than C++, since the latter uses cout from iostream.h

printf("%vartype1%vartype2...%vartypeN", varname1, varname2,...,varnameN);

vartype
d = integers
f = floating point numbers
c = characters
s = strings

varname should be self-explanatory
 

jmcoreymv

Diamond Member
Oct 9, 1999
4,264
0
0
If printf has to print constants, then how could you accept a variable, and then print that variable out again.
 

FatAlbo

Golden Member
May 11, 2000
1,423
0
0
Something like this?

int age=20, count=205;
printf("I am %d years old and have made %d posts here.", age, count);

output: I am 20 years old and have made 205 posts here.
 

Pretender

Banned
Mar 14, 2000
7,192
0
0
int number;

scanf("%d", &number);
printf("The number you just entered was %d\n", number);
 

FatAlbo

Golden Member
May 11, 2000
1,423
0
0
scanf("%vartype", &varname);
Basically the same syntax as printf(). However, remember to include the address-of operator, &.

example:
#include <stdio.h>

int main( void )
{
int age;

printf( &quot;How old are you?\n&quot; );
scanf ( &quot;%d&quot;, &amp;age );
printf( &quot;\nYou are %d years old&quot;, age );

return 0;
}

output:
How old are you?
10

You are 10 years old



<edit>
Pretender: Beat me by a minute! :p
</edit>
 

jmcoreymv

Diamond Member
Oct 9, 1999
4,264
0
0
Oh, i get how ti works now, which is the standard command for c++ cin/cout or printf/scanf? It seems like the printf/scanf is alot more complex although more powerful.
 

Pretender

Banned
Mar 14, 2000
7,192
0
0
I've always used printf/scanf, although my class (AP Computer Science) likes to use (or, rather, is based around) cout/cin. I don't know which one is the standard, but if I had a choice, I'd be using printf/scanf because it just makes logical sense to me. The way cout works doesn't even seem to belong in C/C++ imho.
 

stomp

Senior member
Oct 9, 1999
769
0
0
iostream functions allow a basic understanding of OOP, that is why they tend to be exclusive to C++. istreams and ostreams are inhereted streams, both of which have many member functions. cin/cout are objects of those types... you must remember the << and >> operators are doing the job.

printf() is a function... it is encapsulates nothing more than outputting to stdout.

Its very strange how hard it is to adapt to a strict C compiler after working in C++ for years ... declaring variables at the top of the function, only /* */ comments, etc...
 

FatAlbo

Golden Member
May 11, 2000
1,423
0
0
The standard ANSI C library includes stdio.h but not iostream.h, which means scanf() and printf() are used for input and output. ANSI C also defines comments to be enclosed within /* and */, which means that // will not work on C-only compilers.
 

Pretender

Banned
Mar 14, 2000
7,192
0
0
I've used // on many ansi C compilers, although I guess it's possible that it might not work with some since it's not part of the ansi C standard.
 

jmcoreymv

Diamond Member
Oct 9, 1999
4,264
0
0
Another question, everytime I try to make a prog using the MFC, i cant even figure out where to put my code, i dont even know how to make stuff appear in the main white window, any ideas?
 

jmcoreymv

Diamond Member
Oct 9, 1999
4,264
0
0
LOL, i have a book, but its way to advanced for me and I dont understand a word of it, its the c++ bible, hehe.
 

Pretender

Banned
Mar 14, 2000
7,192
0
0
c++ bible won't be helping you with MFC. I have a book, although I haven't read it in a year. I'll skim the first chapter and get back to you about your original question.
 

jmcoreymv

Diamond Member
Oct 9, 1999
4,264
0
0
Are you sure? cuz its got a pretty big chapter on mfc, but I dont wanna go look at it again, it gives me nightmares.
 

Pretender

Banned
Mar 14, 2000
7,192
0
0
To be honest, I've never read the book, but as far as my knowledge of programming books goes, if it doesn't mention MFC or Visual C++ in the title, it won't be too helpful. This could be the exception, though.