How to read numbers on text files using turbo c++ 4.0? - text-files

I am a beginner in programming and I am trying to make a code that reads 2 numbers from a file and then displays it in the output window on turbo c++.
My code only reads the first number and produces incorrect output for the second number.
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
void main()
{
int x, y;
clrscr();
ifstream inFile;
ofstream outFile;
inFile.open("prac.txt");
while(!inFile.eof())
inFile >> x >> y;
cout << x << " " << y;
inFile.close();
}

#include<iostream.h>
#include<fstream.h>
#include<conio.h>
void main() {
clrscr();
ifstream inFile;
inFile.open("prac.txt");
while(!inFile.eof()) {
int num;
inFile>>num;
cout<<num<<" ";
}
inFile.close();
}

Related

Odd numbers recursively? code currently finds even nums

Just wondering if there is a line change I can make in order to find odds instead of evens? Here's what I got:
#include <iostream>
#include <conio.h>
#include "stdafx.h"
void numOdd(int x, int y)
{
std::cout << x << " ";
if (x < y)
{
numOdd(x + 2, y);
}
}
int main()
{
int x = 0;
int y = 0;
std::cout << "Up to what num to find odd nums? " << std::endl;
std::cin >> y;
numOdd(x, y);
_getch();
}
Initialize x to 1 instead of 0.
Aso, be advised that recursion is not a great fit for this problem; a simple loop would be more appropriate.

printing a board with lines in a 2D char array

#include <iostream>
using namespace std;
//prints the board with labels for options.
void print(const char board[3][3]) //function prototype
{
for(int Row = 0; Row < 3; ++Row)
{
for(int Col =0; Col < 3; ++Col)
{
cout << board[Row][Col],;
cout << " | ";
}
}
}
int main()
{
int MaxiBoardGrid = 9;
char board[3][3];
board[0][0]='A';
board[0][1]='B';
board[0][2]='C';
board[1][0]='D';
board[1][1]='E';
board[1][2]='F';
board[2][0]='G';
board[2][1]='H';
board[2][2]='I';
print(board);
return 0;
}
DESIRED OUTPUT;
A|B|C|
------
D|E|F
------
G|H|I
I am stuck with having the lines printed and the rows in the format above.
language is C++.
I am new to 2D array and I am trying to use the basic codes I am conversant with to get the board printed. then assign player for my proposed game.
Thanks.

How to iterate a string using while loop in C++?

number = 100010001111111
for (int i=0; number.length(); i++) {
while number[i] == 1 {
k++;
}
}
I would like to implement a while-loop as a replacement for the for-loop as shown above.
How could I convert this to a while-loop?
Here's a solution for the problem you mentioned in your comment (Problem - 96A)
#include <iostream>
using namespace std;
int main()
{
cout << "Please enter your players situation" << endl;
std::string str;
cin >> str;
std::string::size_type i = 0;
int NumbersofAppearances = 0;
int ConsectiveNumberSequence = 7; //You can change that to whatever sequence you like
bool IsDangerous=false;
while (i < str.size())
{
if(str[i]=='1' )
{
++NumbersofAppearances;
//We need to check if we reached the consecutive number or not and save it on a different bool variable
if(NumbersofAppearances>=ConsectiveNumberSequence)
IsDangerous=true;
}
else
{
NumbersofAppearances=0;
}
++i;
}
//print out the end result
if (IsDangerous)
cout <<"YES , this is dangerous"<< endl;
else
cout <<"No, this is not dangerous"<< endl;
return 0;
}
And here's a link to Coding ground

istream Unhandled exception, stack overflow

I'm fairly new to C++, and am trying to get the istream to work. I have a class of:
class rat
{
private:
int num;
int denom;
public:
rat();
rat(const int&, const int&);
rat(const int&);
friend ostream& operator << (ostream&, const rat&);
friend istream& operator >> (istream&, const rat&);
};
rat::rat(void)
{
num = 0;
denom = 1;
}
rat::rat(const int &n, const int &d)
{
num = n;
denom = d;
simplify();
}
rat::rat(const int &n)
{
num = n;
denom = 1;
}
ostream& operator << (ostream &os, const rat &r1)
{
os << r1.num;
os << "/";
os << r1.denom;
return os;
}
istream& operator >> (istream &is, const rat &r1)
{
is >> r1.num;
is >> r1.denom;
return is;
}
I also have a .cpp of:
#include <iostream>
#include <conio.h>
using namespace std;
#include "Rats.h"
void main()
{
rat r1(3,4), r2(2,3), r3;
system("cls");
cout << "Please enter a rational number: ";
cin >> r3;
}
My problem occurs whenever it comes across the "is >> r1.num;" line. It gives me the error: Unhandled exception at 0x772d15de in RatClass.exe: 0xC00000FD: Stack overflow.
Again, I'm fairly new, so have not learned what the possible cause could be yet. Any help is appreciated.
Looks like it might be the fact that you're accepting const rat &r1 but by sending data from the istream you would be changing r1. You can't change constants. Not sure if this is the issue but that's the first obvious thing that came to mind.
Try this:
istream& operator >> (istream &is, rat &r1)
{
is >> r1.num;
is >> r1.denom;
return is;
}
Don't forget to change your definition in the class:
friend istream& operator >> (istream&, rat&);

Reading a text file using VC++

I need to read a text file which is for example like bottom :
8.563E+002 2.051E+004 4.180E-004 7.596E-001 5.260E-005 6.898E-002 1.710E-001 8.053E-011 2.686E-013 8.650E-012
each of this 10 scientific digits are the specific value of one line it means each line contains 10 value like above, There is one such line for every grid point in each file. The X indices value most rapidly, then Y, then Z; the first line in the file refers to element (0,0,0); it means the first 10 values presents the first line which refers to element (0,0,0) and the second line (second 10 values) to second element (1,0,0); the last to element (599,247,247).
I don't know how can I write the code for this file using visual C++ ,what I know is I have to read this file line by line which can be determined by eliminating 10 values and tokenize it , then I have to create the x y z for each line il end of the line. I know the concept but I don't know How can I code it in visual C++ .. I need to submit it as my homework .. I really welcome every help .. Thanks
core part can look like:
std::ifstream is("test.txt");
std::vector<double> numbers;
for(;;) {
double number;
is >> number;
if (!is)
break;
numbers.push_back(number);
}
I do not have here MSVC but GCC 4.3. I hope this code helps:
#include <iostream>
#include <fstream>
#include <list>
#include <string>
#include <iterator>
using namespace std;
class customdata
{
friend istream& operator>>(istream& in, customdata& o);
friend ostream& operator<<(ostream& out, const customdata& i);
public:
customdata()
: x(0), y(0), z(0)
{}
customdata(const customdata& o)
: x(o.x), y(o.y), z(o.z)
{}
customdata& operator=(const customdata& o)
{
if (this != &o)
{
x = o.x;
y = o.y;
z = o.z;
}
return *this;
}
private:
long double x, y, z;
};
istream& operator>>(istream& in, customdata& o)
{
in >> o.x >> o.y >> o.z;
return in;
}
ostream& operator<<(ostream& out, const customdata& i)
{
out << "x=" << i.x << " y=" << i.y << " z=" << i.z;
return out;
}
// Usage: yourexec <infile>
int main(int argc, char** argv)
{
int exitcode=0;
if(argc > 1)
{
ifstream from(argv[1]);
if (!from)
{
cerr << "cannot open input file " << argv[1] << endl;
exitcode=1;
}
else
{
list<customdata> mydata;
copy(istream_iterator<customdata>(from), istream_iterator<customdata>(), back_inserter(mydata));
if(mydata.empty())
{
cerr << "corrupt input data" << endl;
exitcode=3;
}
else
copy(mydata.begin(), mydata.end(), ostream_iterator<customdata>(cout, "\n"));
}
}
else
{
cerr << "insufficient calling parameters" << endl;
exitcode=2;
}
return exitcode;
}

Resources