How do I use %d or %c... in Cout instruction?

Asked

Viewed 701 times

-1

I am studying C and C++ and would like to understand how I could transform this code written with printf() in cout, or how to use the functions %d %x %c, etc. in the cout.

int main()
{ 
printf("i make %d program \n" , 2)
system("PAUSE");
return 0;
}

2 answers

6

You must study stream of io and their handlers. Example:

#include <iostream>
using namespace std;

int main() {
    auto x = 65;
    cout << "i make " << x << " program\n";
    cout << "i make " << hex << x << " program " << endl;
    cout << "i make " << (char)x << " program " << endl;
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

The printf() works with conventions where it replaces a part of the text with something that comes as a parameter, and what’s used there as placeholder determines what to do. C++ uses data flows that line up and produce a result. So he assembles each element and the first item is who receives everything that comes after. The cout is the output to the console.

Obviously knowing the value it’s best to put it all together in one string only. It’s only legal to use this when you don’t know what you’ll get in the placeholder, then in this case we will understand that this x would be a value that comes externally.

The second example sends to a handler before sending to the exit and the hex changes the number output format to hexadecimal, such as %x would.

I also used \n and endl, even though both end a line has differences between them (see link above).

The third example had to convert from number to char at hand. Good C compilers configured appropriately require the same. Note that the stream doesn’t care about the guy who comes. If the guy has a stream configured it will know what to do. All basic language types have, and all types should have some operator stream configured, it’s like a ToString() of other languages.

In the posted documentation there are several other very useful modifiers for data formatting. You can even create your own with a little more knowledge.

  • Got it, you would have a website with a list of variables of this type? , hexadecimal, whole etc...? I need a lot because I have an activity to transform code c in c++.

  • Hexadecimal is not a type is a representation and is in link I passed, a start of the types is in https://en.cppreference.com/w/c/language/type. This is official "almost" documentation. I suggest studying it from end to end. You’re probably studying for a bad book, or you’re not interpreting the text correctly. There’s a lot of traps out there, mostly free stuff. In fact maybe you are having difficulty for lack of a base of other things of the computation, it may be that you are using something didactic, what is very common, in general the material expects that you already have a good base.

  • especially in C++ which is one of the hardest languages to learn.

  • I searched a lot of books that indicate teaching at the beginning , comparing the beginning of the book where I am with youtube videos I find it super similar only changing the fact that the book has exercises , which part you recommend me learn before? a specific topic because really learning c and c++ together is complicated.

  • 3

    C and C++ are very different languages and one of the big problems is that almost everyone teaches as if they were the same, this is already a very basic mistake that shows that the person who is teaching does not know what they are talking about. Very, very common indeed. You have to learn fundamentals, understand how the computer works, what’s behind every aspect of the language, why things are the way they are, terminology, computational concepts. Everybody skips that part and so there’s a lot of zombie programmer out there.

  • It’s just that I really need to understand both as soon as possible, what’s your recommendation? , focus on c and go slow with c++ or some other?

  • I don’t know, depending on what you want, I’d go in one, C’s a lot easier. I know very few people who understand both, I wouldn’t waste time trying. There are some very dedicated and experienced guys who can’t be good at both, just seeing the way you write and do things I think will have difficulty with C that is much simpler. Seeing other comments, I think you’re "lotion" and have no idea what you’re getting yourself into.

  • in life everything is a challenge and nobody is born knowing , I will start by c even I will 'skip' the part of the book that explores c++ and continue in c , thanks for the comments and the links , will serve me a lot!

  • There’s a challenge and sticking your foot in the jackfruit

  • thousand rsrs will give up your right , 10 thousand your left , but you will not give up!!! p

  • 2

    @Vitorgonçalves probably the idea of the comments above is not to make you give up, but to make you "not insist wrong". What counts in perseverance is the effort to understand right and wrong and improve and correct what you do. Do not insist on error, because this is a waste of time. From the moment you say, "I really need to understand both of you as quickly as possible," either: Or as Maniero said, you’re "sticking your foot in the jackfruit," or you either emphasize the "quick POSSIBLE" you said yourself, and slow down a little by doing the basic steps, that there will be worth it.

  • I really exaggerated , is that my college will start now and was a couple of years stationed only working with assembly and maintenance , never learned anything about programming and the course of information technology by grid will be necessary to have a lot of knowledge in this area , I’m studying straight to try to get there and not have too many difficulties.

Show 7 more comments

-1


  • Well, first of all, you need to know that C++, it’s different, it’s not defined by output shapers as %d, %x, or things like that. Instead, you use only the variable and the modelling commands.

Example

int n = 5; Cout<< n;

You print the value 5...

For hexadecimal printing, Cout << Hex << n;

And so on, there are several modelers for C++, you can learn them, on youtube, researching workbook of c++, pdf’s, basic courses..

  • ah got it , the book I’m studying : how to program in c and c++ with visual studio , is confusing me kk , he asked in an exercise to transcribe some numbers with percentage and float to c++ but he just gave the table of c , I’ll have to research everything dnv ;-;

  • 1

    Want to learn c++ from basic to advanced ? search youtube Marco Castro C++

  • the problem is this kkk I am using the book as a base for 1 month if I change now I will pocket all kkk

  • Depending on the book, perhaps it is better to exchange, for what I have seen, your book is embossing c with c++, and this will confuse you, as they are languages oriented to totally different topics.

  • Victor , so you recommend learning through the link of the channel you recommended ? , this book explores both a lot , so far I’m understanding a lot , is that I was in a hurry because I need to learn both languages as soon as possible kk and I still have to study java ,

  • Man, if you’re in a hurry, you’ll barely learn the basics, and then you’ll forget a lot of important things!! Do you want to learn real programming in any language ? Create an https://www.urionlinejudge.com account is a programming account with over 1,800 problems from beginner to beginner. Dude, one thing, if you want to be a "good programmer" programmer, do Ri.. your logic will be amazing.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.