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?
You are mixing up C++ and C output styles. Change your fprintfs to look like:
cout << "value is: " << i << "\n";
std::fprintf(stdout, )
std :: cout没有FILE *类型。
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
.
You are mixin C and C++ incorrectly. Use only 1, and stick to it untill you learn what the difference between the types are.
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.