- I am having trouble with the headers -Also if I remove the .h it doesn\'t allo
ID: 3771049 • Letter: #
Question
- I am having trouble with the headers
-Also if I remove the .h it doesn't allow the program to build, if kept it brings up warnings (Also clrcr is coming up undefined)
Q: How can I execute this program on unix using C++ language? (Unix doesnt like iostream.h)
Here is my source code:
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
clrscr();
long bin, dec = 0, rem, num, base = 1;
int i=0;
ifstream myReadFile;
myReadFile.open("text.txt");
char *output;
char mystr[999];
// if (myReadFile.is_open())
{
i=0;
while (!myReadFile.eof()) {
myReadFile >> output;
num = atol(output);
bin = num;
cout << bin;
while (num > 0)
{
rem = num % 10;
dec = dec + rem * base;
base = base * 2;
num = num / 10;
}
if(dec > 64 || dec < 91 || dec > 96 || dec <123)
{ mystr[i] = (char) dec;
i++ ;}
* output =' ';
dec=0; base =1;
}
}
myReadFile.close();
cout << mystr;
return 0;
}
Explanation / Answer
use
#include <bits/stdc++.h>
using namespace std;
instead of
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<stdlib.h>
#include <bits/stdc++.h> is basically a header file that also includes every standard library and stl include file.
use
system("clear");
instead of
clrscr();
Finally a suggession:
There is a better way and simple way to read data from a file.
You can replace
ifstream myReadFile;
myReadFile.open("text.txt");
char *output;
while (!myReadFile.eof()) {
myReadFile >> output;
num = atol(output);
}
BY
ifstream myReadFile;
myReadFile.open("text.txt");
while(myReadFile>>num){
//use num in here
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.