- #1
ActionPotential
- 60
- 4
Homework Statement
Hey guys! Thanks for taking the time to help me out, I really appreciate it. I am in my second semester of college, taking my first C++ course, and I had never programmed before this class so I am kind of stuck on a problem. I have almost the entire assignment figured out and I am confident I can get this to work with a bit of guidance so any hints would be wonderful!
The basic idea is that the program will read in data from a text file using fstream and store it as a char. As each character is read in, an isdigit acid test determines if it is a digit from 0-9 and then proceeds to this assignment (if not, then we ignore it):
sum = (10*sum)+ch
Ex.
Code:
char ch;
int sum;
sum= 0;
ch = 3;
sum = 10*0+3 == 3;
ch = 9;
sum = 10*3 + 9 == 39;
ch = 7;
sum = 10*39+7 == 397;
For example, in my data file:
@2,89#3,*67
$187,3#%34,72#123#
*3*7*1#2,3,8
The algorithm would process each individual character, assigning its value to sum, then move to the next one until it hits a '\n' or a '#'. The ouput after full processing would look like this:
289
367
1873
3472
123
371
238
Now, I can think of a variety of ways that I could make this work, however, our teacher requires this algorithm to be based on his sample algorithm as well as using the idea of summing the characters together to produce the desired number.
Homework Equations
We are required to use an EOF Loop, process each character individually and use a sum formula to properly output the numbers separated by '#'.
The Attempt at a Solution
Code:
/* CS 201 - Computer Science 1
Program 1 - Version D - Character Filtration
February 15, 2014 */
#include <iostream>
#include <fstream>
#include <cctype>
using namespace std;
int main ()
{
/* Variable/Data Type Declarations */
char ch;
int sum;
ifstream data;
ofstream out;
/* Input/Output of File Stream Data */
data.open ("numbers.txt");
if(not data){
cout << "Failure to open numbers.txt" << endl;
system ("pause");
return 1;}
out.open ("out.txt");
if(not out){
cout << "failure to open out.txt" << endl;
system ("pause");
return 1;}
sum = 0; // Initialization of Sum
data.get(ch); // Priming Read for End-of-File Loop
/* External EOF Loop */
while(data){
/* Internal Control Structure and Processing */
while((ch !='\n') and data) {
if(isdigit(ch)){ // All Processing Here
sum = 10*sum+ch; // Character Sum Assignment
data.get(ch); // Update for Inner Loop
}
if(ch == '#'){ //Decision to Print Sum to File
out << "The Sum is: " << sum; //Prints Sum to out.txt
sum = 0; //Resets the Sum
data.get(ch); //Retrieves next char in istream
}
} // Inner Loop
} // Outer Loop
cout << "Your data has been processed and is available for use. \n " << endl;
system("pause");
data.close(); out.close(); // Close Data Files
return 0;
} // main
I have tried A LOT of different things and I just feel I am missing something really simple. This isn't a difficult problem to program but I think because of the specific conditions we have to apply to the loops per the teacher, it is making it more writing myself out of a corner.
Thanks for any and all input.
Last edited: