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
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
#include
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;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started