I dont get it!

This is a discussion on I dont get it! within the C++ Programming forums, part of the General Programming Boards category; Ok, I was given an assignment for Computer Science AP to write a program to: 1. In function main, open ...

  1. #1
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533

    I dont get it!

    Ok, I was given an assignment for Computer Science AP to write a program to:

    1. In function main, open two file streams:
    a. An ifstream linked to the original text file, the before version
    b. an ofstream linked to a new file name, the after version

    2. the program will remove the blanks begining if each line and replace them with an integer count of the number of blanks present. Use a setw(2) format-specifier to print the integer. Two spaces should follow after integer follow, then the rest of the line should be transferred unchanged.

    here is what I have, but it only parses through once and comes up with bogus output because of it.

    got any ideas?

    help will be EXTREMELY appreciated

    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>
    #include <iomanip.h>
    int main(int argc, char* argv[])
    {
    	if(argc>1)
    		abort();
    	char a;
    	int counter=1,lines=1, spaces[256], linenum=1;
    	ifstream in;
    	ofstream out;
    	in.open("c:\\luke\\new.txt");
    	out.open("c:\\luke\\new2.txt");
    
    	while(in.get(a))
    	{	if(a=='\n')
    			lines++;
    	}
    	while(in.get(a))
    	{
    		if(a==' ')
    			spaces[linenum]++;
    		else if(a == '\t')
    			spaces[linenum]+=4;
    
    		if(a=='\n')
    			linenum++;
    	}
    
    	out<<spaces[1]<<setw(2);
    	cout<<lines<<endl<<counter<<endl<<spaces[1]<<endl;
    	while(counter<=lines && in.get(a))
    	{
    		if(a=='\n')
    		{
    			counter++;
    			out<<endl<<spaces[counter]<<setw(2);
    		}
    		else if(a=='\t')
    		{
    		}
    		else
    			out<<a;
    	}
    	in.close();
    	out.close();
    	return 0;
    }
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    How about this. Why don't you read a full line at a time and then parse through it? I think this would be much easier. You can read until a non-space character is reached and use that counter for the white space count. I think this would be easier.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    yah I tried that, here is the problem
    I parsed through each line but at the beginig is where I have to hace a space number like:
    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>
    int main(int argc, char* argv[])
    {
    	if(argc>1)
    		abort();
    	char a;
    	int count=0,lines=0;
    	ifstream in;
    	ofstream out;
    	in.open("c:\\luke\\new.txt");
    	out.open("c:\\luke\\new2.txt");
    	while(in.get(a))
    	{	if(a=='\n')
    			lines++;
    	}
    	cout<<lines<<endl;
    	in.close();
    	out.close();
    	return 0;
    }
    will become
    Code:
    0 #include <iostream.h>
    0 #include <fstream.h>
    0 #include <stdlib.h>
    0 int main(int argc, char* argv[])
    0 {
    3 if(argc>1)
    6 abort();
    3 char a;
    3 int count=0,lines=0;
    3 ifstream in;
    3 ofstream out;
    3 in.open("c:\\luke\\new.txt");
    3 out.open("c:\\luke\\new2.txt");
    3 while(in.get(a))
    3 {	if(a=='\n')
    3 lines++;
    3 }
    3 cout<<lines<<endl;
    3 in.close();
    3 out.close();
    3 return 0;
    0 }
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    and the problem is???

    You read in the line to a buffer....then you parse through detecting the number of spaces...then you write the correct output to a new file. I don't see where the problem is.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    you know the funny thing is that I knew C++ before I went into this class, and he hasn't taught us arrays yet, I am curious how to do it otherwise.
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  6. #6
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    ok that all work nice and dandy!
    but now I have the WHOLE file in a buffer
    and im going through the lines
    how do I only take the spaces on the FIRST PART and leave the rest alone until I hit a newline.

    The english sounds extrememly similar to the code but when I try it, it doesn't work...
    I am stumped
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Don't read the whole file into a buffer. Just read one line at a time.

  8. #8
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    k here is what I got for reading line=by=line but it doesn't work for the parsing:
    Code:
    	while(in.getline(buff,1024,'\n'))
    	{
    		
    
    		count=0;
    		i=0;
    		while(buff[i]=='\t')
    		{
    			count++;
    		}
    		out<<setw(2)<<count<<" ";
    		i=0;
    		while(buff[i]!='\0')
    		{	cout<<buff[i];
    			i++;
    		}
    		out<<endl;
    	}
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  9. #9
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    I DID IT HURRAY!!!!!!!!!!!!!!!!
    YAY
    Thank you MrWizard
    you have been a great help

    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>
    #include <iomanip.h>
    #include <ctype.h>
    int main(int argc, char* argv[])
    {
    	if(argc>1)
    		abort();
    
    
    	char buff[20000];
    
    	int i=0,count=0;
    	ifstream in;
    	ofstream out;
    	in.open("c:\\luke\\new.txt");
    	out.open("c:\\luke\\new2.txt");
    	while(in.getline(buff,1024,'\n'))
    	{
    		
    
    		count=0;
    		i=0;
    		do
    		{
    
    		if(buff[i]=='\t')
    		{
    			count+=3;
    			
    		}
    		i++;
    		} while(! isgraph(buff[i]));
    		out<<setw(2)<<count<<" ";
    		i=0;
    		i=count/3;
    		while(buff[i]!='\0')
    		{	
    		
    			out<<buff[i];
    			i++;
    		}
    		out<<endl;
    	}
    
    
    
    
    	return 0;
    }
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  10. #10
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Code:
    while(buff[i]!='\0')
    {	
       out<<buff[i];
       i++;
    }
    Has the same effect as:
    Code:
    out << &buf[i];
    B.t.w. Tab isn't the only blank, what about a space?

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21