0

I am new learning C++ and I have basic questions and basic troubles :(

I want to print a list of numbers that are coming from the next following while condition:

int list=0;
while (list<100){
    list=list+r;
}

I want to use printf instead of cout (because I still don´t know why with cout is not working).

Can anyone help me to give me the analogous printf command to

cout<<list<<"\t";

Thanks a lot!!!

angelavtc
  • 41
  • 1
  • 6

2 Answers2

0

Here is a small sample program which counts up to 100 in increments of 10.

I use both std::cout and printf to display the value of list in each increment.

Comments added to hopefully help aid you in learning

#include <iostream>
#include <cstdio>

int main()
{
    int r = 10;

    int list=0;
    while (list < 100)
    {
        list += r;                 // this is the same as saying list = list + r, but is more succinct

        std::cout << list << "\t"; // cout is in the std namespace, so you have to prefix with std::

        printf("%d\n", list);      // the printf format specified for int is "%d"
    }
}

Output:

10    10
20    20
30    30
40    40
50    50
60    60
70    70
80    80
90    90
100   100

Please note that I didn't use using namespace std; at the top to import cout into the global namespace. IMHO this is bad practice, so I typically will prefer std::cout etc.

Steve Lorimer
  • 27,059
  • 17
  • 118
  • 213
  • 1
    You appear not to have included _any_ header to get the declaration of printf(). –  Mar 04 '17 at 16:31
  • @NeilButterworth it is indirectly available from ``. [Working example here](http://coliru.stacked-crooked.com/a/a5283301de9a7221) – Steve Lorimer Mar 04 '17 at 16:40
  • 1
    You must not depend on such things - the standard says that any header _may_ include another header, but it doesn't say that any header _must_ include another one. If you use a function from the Standard Library, you should always include the header that the Standard says declares it. –  Mar 04 '17 at 16:43
  • @SteveLorimer thanks a lot for your answer, it was really healpful. However I still have a doubt how would I change that code if what I have on the list are numbers with decimals? I tried to change the int for a double but it is not working. Any suggestions? Thanks again – angelavtc Mar 04 '17 at 19:16
  • @angelavtc you really should ask another question, or update your question to reflect the new requirements. SO is not a forum, but a Q&A site. However, in this case `std::cout` will output decimal without any change. For `printf` you want to use either `%f` or `%g`. Please read more [here](http://en.cppreference.com/w/cpp/io/c/fprintf) – Steve Lorimer Mar 04 '17 at 19:17
  • @SteveLoremer got it. Thank you! – angelavtc Mar 04 '17 at 19:21
0

printf is a C function, not a C++ one, if you are learning C++ you should try to solve the problem with std::cout, which is the usual way of printing.

Anyway, printf is pretty simple to use, it takes a first argument that is a string (a C string, so an array of char with the last char as a '\0' character) and as many parameters as you have format specifiers in your string (a format specifiers is a % char followed by another char that the function what time the variable is going to be)

Examples:

int intvat;
char charvar;
float floatvar;
char* stringvar; // to print it the last char of stringvar must be \0
printf("this is an int: %d", intvar);
printf("this is a char: %c", charvar);
printf("this is a string: %s", stringvar);
printf("this is a float and an int: %f, %d", floatvar, intvar);

For more information about printf you can refer to the reference page here: http://www.cplusplus.com/reference/cstdio/printf/

bracco23
  • 2,181
  • 10
  • 28
  • I don't understand. The C++ language includes `printf`, so I believe that `printf` is a C++ function. Please cite your reference that says that `printf` is not a C++ function. – Thomas Matthews Mar 04 '17 at 17:33
  • As anything that comes from the `cstdio`, `printf` is a C function which has been included in the C++ standard libraries for compatibility (One of the main goal was that every C code should be legal C++ code, or become such with as little modifications as possible). One reference is http://stackoverflow.com/questions/2872543/printf-vs-cout-in-c , another reference is the same site in the answer, http://www.cplusplus.com/reference/clibrary/ here it talks of all the C headers in C++, among which there is `cstdio`, where `printf` is defined – bracco23 Mar 04 '17 at 19:02
  • @bracco23 thanks a lot for your answer, it was really useful :) – angelavtc Mar 04 '17 at 19:17