Visual studios c++ I want to write the data i get from my loop into a data file.
ID: 3875210 • Letter: V
Question
Visual studios c++
I want to write the data i get from my loop into a data file. I cant seem to get the code write. I don't really know where i went wrong.
#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream outDataFile;
outDataFile.open("Ch 5 Values.txt");
cout << " Now writing data to the file. ";
int somenumber;
char answer;
cout << " Please enter a four digit integer that does not contain zeros." << endl;
cin >> somenumber; //restriction for simplicity
{
while (somenumber <= 1000) //varifies the input
{ //meets my restriction
cout << " Invalid response. I will keep adding to your number until a new acceptable number is reached," << endl;
somenumber++; //modifies the input if not valid
cout << " The new number is :" << somenumber << endl;
}
}
//revresi
if (somenumber > 1000 && somenumber < 9999) //checks the variable again
{
int firstdigit = somenumber / 1000; //determines the first number
somenumber %= 1000; // gets the remainder 3 numbers
int seconddigit = somenumber / 100;
somenumber %= 100;
int thirddigit = somenumber / 10;
int fourthdigit = somenumber % 10;
int newfirst = fourthdigit * 1000;
int newsecond = thirddigit * 100;
int newthird = seconddigit * 10;
int newfourth = firstdigit;
int reversednum = newfirst + newsecond + newthird + newfourth;
cout << "The new number is " << reversednum << endl;
//even or odd
if (firstdigit % 2 == 0) //checks to see if the number when divided by 2
cout << firstdigit << " is even." << endl; // has no remainders. Hence even.
else
cout << firstdigit << " is odd." << endl;
if (seconddigit % 2 == 0)
cout << seconddigit << " is even." << endl;
else
cout << seconddigit << " is odd." << endl;
if (thirddigit % 2 == 0)
cout << thirddigit << " is even." << endl;
else
cout << thirddigit << " is odd." << endl;
if (fourthdigit % 2 == 0)
cout << fourthdigit << " is even." << endl;
else
cout << fourthdigit << " is odd." << endl;
}
cout << " Would you like to repeat this process ( Y/N )" << endl;
cin >> answer;
//Loop
while (answer == 'Y' || answer == 'y')
{
cout << " Please enter a four digit integer that does not contain zeros." << endl;
cin >> somenumber;
{
while (somenumber <= 1000)
{
cout << " Invalid response. I will keep adding to your number until a new acceptable number is reached," << endl;
somenumber++;
cout << " The new number is :" << somenumber << endl;
}
}
//revresi
if (somenumber > 1000 && somenumber < 9999)
{
int firstdigit = somenumber / 1000;
somenumber %= 1000;
int seconddigit = somenumber / 100;
somenumber %= 100;
int thirddigit = somenumber / 10;
int fourthdigit = somenumber % 10;
int newfirst = fourthdigit * 1000;
int newsecond = thirddigit * 100;
int newthird = seconddigit * 10;
int newfourth = firstdigit;
int reversednum = newfirst + newsecond + newthird + newfourth;
cout << "The new number is " << reversednum << endl;
//even or odd
if (firstdigit % 2 == 0) //checks to see if the number when divided by 2
cout << firstdigit << " is even." << endl; // has no remainders. Hence even.
else
cout << firstdigit << " is odd." << endl;
if (seconddigit % 2 == 0)
cout << seconddigit << " is even." << endl;
else
cout << seconddigit << " is odd." << endl;
if (thirddigit % 2 == 0)
cout << thirddigit << " is even." << endl;
else
cout << thirddigit << " is odd." << endl;
if (fourthdigit % 2 == 0)
cout << fourthdigit << " is even." << endl;
else
cout << fourthdigit << " is odd." << endl;
}
cout << " Would you like to repeat this process ( Y/N )" << endl;
cin >> answer;
}
outDataFile.close();
cout << "Done. ";
system("pause");
return 0;
Explanation / Answer
<<I want to write the data i get from my loop into a data file. I cant seem to get the code write. I don't really know where i went wrong.>>
If the data is not going to the file , it is because there is not a single statement in
the while loop writing to the file or even outside the file.The file has been opened
for writing but there is no write statement to file. Please add the statement :
outDataFile << "whatever you want to write"
or if you want to write every thing you are displaying to the output screen , then
whereever you are having cout statement just repeat the statement with outDataFile.
For example :
if you have:
cout << "The new number is " << reversednum << endl;
Then just repeat the same statement with outDataFile as
outDataFile << "The new number is " << reversednum << endl;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.