views:

1198

answers:

6

I'm getting compile error in this code

    #include<iostream>
    #include<cstdio>
    #include<string>
    using namespace std;
    void main(int argc,char *argv[])
    {
        int i;
        for(i = 0;i<10;i++)
           fprintf(cout,"%d\n",i);
        fprintf(cout,"abc:\n");
        string s;
        cin>>s;
        if(s == "resume") { 
            for(i = 0;i<10;i++)
            fprintf(cout,"%d\n",i);
        }
   }

Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.21022.08 for 80x86 Copyright (C) Microsoft Corporation. All rights reserved.

try.cpp C:\Program Files\Microsoft Visual Studio 9.0\VC\INCLUDE\xlocale(342) : warning C 4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc

try.cpp(9) : error C2664: 'fprintf' : cannot convert parameter 1 from 'std::ostr eam' to 'FILE *' No user-defined-conversion operator available that can perform this conv ersion, or the operator cannot be called

try.cpp(10) : error C2664: 'fprintf' : cannot convert parameter 1 from 'std::ost ream' to 'FILE *' No user-defined-conversion operator available that can perform this conv ersion, or the operator cannot be called

try.cpp(16) : error C2664: 'fprintf' : cannot convert parameter 1 from 'std::ost ream' to 'FILE *' No user-defined-conversion operator available that can perform this conv ersion, or the operator cannot be called

what is wrong?

+6  A: 

You are mixing up C++ and C output styles. Change your fprintfs to look like:

cout << "value is: " << i << "\n";
anon
Or use printf("...",...) instead of fprintf(cout,"...",...)
schnaader
A: 

You are mixin C and C++ incorrectly. Use only 1, and stick to it untill you learn what the difference between the types are.

leppie
+2  A: 

std::cout has no FILE* type.

bb
+4  A: 
std::fprintf(stdout, )
Mykola Golubyev
+1  A: 

Alternately, change your includes to:

#include <stdio.h>
#include <string.h>

and the fprintf() calls to

fprintf(stdout,"abc:\n");

Then you're talking C.

gimel
in c++ it is better to include <cstdio>
Mykola Golubyev
I was spelling out the *C* alternative - not C++.
gimel
A: 

Here's your code without compilation errors:

#include <iostream>
#include <string>

int main()
{
  using namespace std;

  for(int i = 0; i < 10; i++)
    cout << i << '\n';
  cout << "abc" << endl;

  string s;
  cin >> s;
  if(s == "resume") 
    for(int i = 0; i < 10; i++)
      cout << i << '\n';

  return 0;
}
J.F. Sebastian