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!
</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.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |