Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > c++ begginer program help

Reply
Thread Tools

c++ begginer program help

 
 
srenusa@yahoo.com
Guest
Posts: n/a
 
      04-04-2007
hi, I am quite new to c++ programming and I would need some help with
my college assigment:
write a program which reads input c++ file and removes commentary. The
commentary can be oneline(//) and block commentary(/**/. The name of
the file is read from the command line. use functions and global
variables if neccesary.
here is my code. Thanks in advance


#include <fstream>
#include <stdlib.h>
#include <iostream.h>
#include <stdio.h>
ifstream fp;
ofstream fpp;

void remove (int kraj, char *line){ //removes '//' and saves to file
int i;
for (i = 0; i <= kraj; i++) {
fpp<<line;
}
}
void proceed (int kkraj, char *line,int lenght){
int i;
for (i = kkraj; i<=lenght; i++) {
fpp<<line;
}

}


void main(){
int i ,k, k2,k3;
char filename[12];
char *line;
cout<<"what is the name of the file";
cin>>filename;
fp.open (filename, ios :: in); //file to read from
if (!fp){
cout<<"\n error opening\n";
exit(0);
}
fpp.open("no commentary", ios:ut); //file to save non commentary
program to
if (!fpp){
cout << "Error opening file.\n";
}


while(line=fgets(fp)){
for(i=0; i<strlen(line); i++){
if ((line[i]=='/')&&(line[i+1]=='/')) {
k=i-1;
remove(k, line);

}
if ((line[i]=='/')&&(!begin)&&(line[i+1]=='*')) {
k2=i-1;
remove(k2, line);
begin=1;

}
if ((line[i]=='*')&&(begin)&&(line[i+1]=='/')){
k3=i-1;
begin=0;
proceed(k3, line, strlen(line)) ;
}

}
fp.close() ;
fpp.close() ;
return;
}

}

 
Reply With Quote
 
 
 
 
osmium
Guest
Posts: n/a
 
      04-04-2007
srenusa wrote:

> hi, I am quite new to c++ programming and I would need some help with
> my college assigment:
> write a program which reads input c++ file and removes commentary. The
> commentary can be oneline(//) and block commentary(/**/. The name of
> the file is read from the command line. use functions and global
> variables if neccesary.
> here is my code. Thanks in advance


What kind of help do you want? I didn't try, but I would not expect this to
compile.
I would like to see descriptions as to what the remove and proceed functions
are intended to perform. I would like to see indentation. Does your program
allow for the fact the /* and the matching */ can be on different lines?

<snippage>

>void remove (int kraj, char *line){ //removes '//' and saves to file


Surely not!? The instructions were to remove the commentary, not the thing
that indicates it *is* a commentary

> fpp.open("no commentary", ios:ut); //file to save non commentary


I'm basing what I said on not compiling on that line. I think it should be
ios:ut. *Two* changes I just scanned the code idly to see if I thought it
might compile.



 
Reply With Quote
 
 
 
 
srenusa@yahoo.com
Guest
Posts: n/a
 
      04-04-2007

I have a problem with mismatching: cannot convert ifstream to char:
while(line=fgets(fp)){
for(i=0; i<signed(strlen(line)); i++){


void remove (int kraj, char *line){ //removes '//' and saves to file
I intended this function to write to a new file all characters
until //
characters are placed in char line and I thought that line[1]
represents the first character.


fpp.open("no commentary", ios:ut); //file to save non commentary
tipfeller


thx for your help

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      04-04-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I have a problem with mismatching: cannot convert ifstream to char:
> while(line=fgets(fp)){


What is 'line=fgets(fp)' supposed to do? Perhaps you need to RTFM
about 'fgets' and 'ifstream'...

> for(i=0; i<signed(strlen(line)); i++){
> [..]


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      04-04-2007
srenusa wrote:

<snippage>

> #include <fstream>
> #include <stdlib.h>
> #include <iostream.h>
> #include <stdio.h>
> ifstream fp;
> ofstream fpp;
>
> void main(){
> int i ,k, k2,k3;
> char filename[12];
> char *line;
> cout<<"what is the name of the file";
> cin>>filename;
> fp.open (filename, ios :: in); //file to read from
> if (!fp){
> cout<<"\n error opening\n";
> exit(0);
> }
> fpp.open("no commentary", ios:ut); //file to save non commentary
> program to
> if (!fpp){
> cout << "Error opening file.\n";
> }
>
>
> while(line=fgets(fp)){


line is a char* pointing to God knows where. You are off to a bad start.
There are better ways of doing this. Look into

string line;
getline(fin, line): // where fiin is a file that is open for input
if(!cin)
// almost certainly EOF
else
// do something wise and Good with line.


 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      04-04-2007
"osmium" wrote:

> srenusa wrote:
>
> <snippage>
>
>> #include <fstream>
>> #include <stdlib.h>
>> #include <iostream.h>
>> #include <stdio.h>
>> ifstream fp;
>> ofstream fpp;
>>
>> void main(){
>> int i ,k, k2,k3;
>> char filename[12];
>> char *line;
>> cout<<"what is the name of the file";
>> cin>>filename;
>> fp.open (filename, ios :: in); //file to read from
>> if (!fp){
>> cout<<"\n error opening\n";
>> exit(0);
>> }
>> fpp.open("no commentary", ios:ut); //file to save non commentary
>> program to
>> if (!fpp){
>> cout << "Error opening file.\n";
>> }
>>
>>
>> while(line=fgets(fp)){

>
> line is a char* pointing to God knows where. You are off to a bad start.
> There are better ways of doing this. Look into
>
> string line;
> getline(fin, line): // where fiin is a file that is open for input
> if(!cin)


Should be:
if(!fin)

> // almost certainly EOF
> else
> // do something wise and Good with line.
>
>



 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      04-05-2007
(E-Mail Removed) wrote:
> hi, I am quite new to c++ programming and I would need some help with
> my college assigment:
> write a program which reads input c++ file and removes commentary. The
> commentary can be oneline(//) and block commentary(/**/. The name of
> the file is read from the command line. use functions and global
> variables if neccesary.
> here is my code. Thanks in advance
>
>
> #include <fstream>
> #include <stdlib.h>
> #include <iostream.h>

Non-standard header. Use <iostream>

> #include <stdio.h>
> ifstream fp;
> ofstream fpp;
>
> void remove (int kraj, char *line){ //removes '//' and saves to file
> int i;
> for (i = 0; i <= kraj; i++) {
> fpp<<line;
> }
> }
> void proceed (int kkraj, char *line,int lenght){
> int i;
> for (i = kkraj; i<=lenght; i++) {
> fpp<<line;
> }
>
> }
>
>
> void main(){

void main is not C++. main() always returns int.


> int i ,k, k2,k3;
> char filename[12];
> char *line;
> cout<<"what is the name of the file";
> cin>>filename;
> fp.open (filename, ios :: in); //file to read from
> if (!fp){
> cout<<"\n error opening\n";
> exit(0);
> }
> fpp.open("no commentary", ios:ut); //file to save non commentary
> program to
> if (!fpp){
> cout << "Error opening file.\n";
> }
>
>
> while(line=fgets(fp)){
> for(i=0; i<strlen(line); i++){
> if ((line[i]=='/')&&(line[i+1]=='/')) {
> k=i-1;
> remove(k, line);
>
> }
> if ((line[i]=='/')&&(!begin)&&(line[i+1]=='*')) {
> k2=i-1;
> remove(k2, line);
> begin=1;
>
> }
> if ((line[i]=='*')&&(begin)&&(line[i+1]=='/')){
> k3=i-1;
> begin=0;
> proceed(k3, line, strlen(line)) ;
> }
>
> }
> fp.close() ;
> fpp.close() ;
> return;
> }
>
> }
>

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-05-2007
On Apr 4, 9:34 pm, "osmium" <(E-Mail Removed)> wrote:
> srenusa wrote:


> string line;
> getline(fin, line): // where fiin is a file that is open for input
> if(!cin)
> // almost certainly EOF
> else
> // do something wise and Good with line.


This is generally good advice, but I'm wondering. Given that
one of the forms of comments ("/*...*/") can span multiple
lines, I wonder if it wouldn't be better to read character by
character, and use a state machine of some sort. Or use simple
queue (e.g. std::deque) to keep a sliding window of two
characters in the file.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      04-06-2007
Comments in line

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> hi, I am quite new to c++ programming and I would need some help with
> my college assigment:
> write a program which reads input c++ file and removes commentary. The
> commentary can be oneline(//) and block commentary(/**/. The name of
> the file is read from the command line. use functions and global
> variables if neccesary.
> here is my code. Thanks in advance
>
>
> #include <fstream>
> #include <stdlib.h>
> #include <iostream.h>
> #include <stdio.h>
> ifstream fp;
> ofstream fpp;
>
> void remove (int kraj, char *line){ //removes '//' and saves to file


Comment, or function, is wrong. It should remove the // and everything
after it.
I.E. If it was processing this file, for your line above:
void remove (int kraj, char *line){ //removes '//' and saves to file
it should write:
void remove (int kraj, char *line){

So basically you want to write everying up to the //

forget about char* and use std::string, then you can use find, will make
your life a LOT easier.

In otherwords, rewrite this function using std::string, or just move it to
mainline (it's like 2 lines of code

> int i;
> for (i = 0; i <= kraj; i++) {
> fpp<<line;
> }
> }


> void proceed (int kkraj, char *line,int lenght){


No comment, but I take it this function is supposed to write everything
*after* kkraj to the file.

> int i;
> for (i = kkraj; i<=lenght; i++) {
> fpp<<line;
> }
>
> }
>
>
> void main(){
> int i ,k, k2,k3;
> char filename[12];


12 characters is no where near long enough. forget about char arrays and
use std::string

std::string filename;

> char *line;


This is a pointer. But it's not initialized so is pointing to... anything.
Somewhere in memory where most likely you don't want to write. use
std::string and you wont' have a problem.

std::string line;

> cout<<"what is the name of the file";
> cin>>filename;
> fp.open (filename, ios :: in); //file to read from


open takes a char*, and if you make filename a std::string you'd need to do:
fp.open( filename.c_str(), ios::in )

> if (!fp){
> cout<<"\n error opening\n";
> exit(0);
> }
> fpp.open("no commentary", ios:ut); //file to save non commentary
> program to
> if (!fpp){
> cout << "Error opening file.\n";
> }
>
>
> while(line=fgets(fp)){


How about:
while ( std::getline( line ) )

> for(i=0; i<strlen(line); i++){
> if ((line[i]=='/')&&(line[i+1]=='/')) {


You can use find() here if you are using a std::string to get the position.
std::string::size_type k = line.find("//");

To see if it found it, compare it with std::string::npos.

if ( k != std::string::npos )
// ...

Now, what you really want at this point is just to keep everything up to the
//
I'd just use substr
line = substr( line, k );
Now the // and everything past it are gone.

> k=i-1;
> remove(k, line);


Ahh, did you really want to do this? What if you are in the middle of a
block comment? Then you don't want to write this line. You should check to
see about block comments before worrying about the line themselves. Move
this section down below the block comment part.

>
> }
> if ((line[i]=='/')&&(!begin)&&(line[i+1]=='*')) {
> k2=i-1;
> remove(k2, line);
> begin=1;


begin = 1
Why not use C++'s bool?
begin = true;
But, of course, you never declared begin anywhere, did you?
And begin isn't very descriptive of what this variable does either. So
before while do something like:
bool BlockComment = false;

here you would do
BlockComment = true;

> }
> if ((line[i]=='*')&&(begin)&&(line[i+1]=='/')){


Again, with std::string this becomes

> k3=i-1;
> begin=0;
> proceed(k3, line, strlen(line)) ;
> }
>
> }
> fp.close() ;
> fpp.close() ;
> return;
> }
>
> }


Hmm.. I dont' see where you checked for ending a block comment, or where you
are not writing anything if you started a block comment.

If you read thourgh the comments people provided and look at your code you
should be able to figure it out. You have the logic about 50% right. Using
chae pointers is making it much more difficult then it needs to be,
std::strings will make it a whole lot easier, especially since then you can
use find, substr and other functions.


 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      04-06-2007

"Jim Langston" <(E-Mail Removed)> wrote in message
news:uToRh.6091$(E-Mail Removed)...
> Comments in line
>
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ups.com...
>> hi, I am quite new to c++ programming and I would need some help with
>> my college assigment:
>> write a program which reads input c++ file and removes commentary. The
>> commentary can be oneline(//) and block commentary(/**/. The name of
>> the file is read from the command line. use functions and global
>> variables if neccesary.
>> here is my code. Thanks in advance
>>
>>
>> #include <fstream>
>> #include <stdlib.h>
>> #include <iostream.h>
>> #include <stdio.h>
>> ifstream fp;
>> ofstream fpp;
>>
>> void remove (int kraj, char *line){ //removes '//' and saves to file

>
> Comment, or function, is wrong. It should remove the // and everything
> after it.
> I.E. If it was processing this file, for your line above:
> void remove (int kraj, char *line){ //removes '//' and saves to file
> it should write:
> void remove (int kraj, char *line){
>
> So basically you want to write everying up to the //
>
> forget about char* and use std::string, then you can use find, will make
> your life a LOT easier.
>
> In otherwords, rewrite this function using std::string, or just move it to
> mainline (it's like 2 lines of code
>
>> int i;
>> for (i = 0; i <= kraj; i++) {
>> fpp<<line;
>> }
>> }

>
>> void proceed (int kkraj, char *line,int lenght){

>
> No comment, but I take it this function is supposed to write everything
> *after* kkraj to the file.
>
>> int i;
>> for (i = kkraj; i<=lenght; i++) {
>> fpp<<line;
>> }
>>
>> }
>>
>>
>> void main(){
>> int i ,k, k2,k3;
>> char filename[12];

>
> 12 characters is no where near long enough. forget about char arrays and
> use std::string
>
> std::string filename;
>
>> char *line;

>
> This is a pointer. But it's not initialized so is pointing to...
> anything. Somewhere in memory where most likely you don't want to write.
> use std::string and you wont' have a problem.
>
> std::string line;
>
>> cout<<"what is the name of the file";
>> cin>>filename;
>> fp.open (filename, ios :: in); //file to read from

>
> open takes a char*, and if you make filename a std::string you'd need to
> do:
> fp.open( filename.c_str(), ios::in )
>
>> if (!fp){
>> cout<<"\n error opening\n";
>> exit(0);
>> }
>> fpp.open("no commentary", ios:ut); //file to save non commentary
>> program to
>> if (!fpp){
>> cout << "Error opening file.\n";
>> }
>>
>>
>> while(line=fgets(fp)){

>
> How about:
> while ( std::getline( line ) )


Sorry, that's
while ( std::getline( fp, line ))

>> for(i=0; i<strlen(line); i++){
>> if ((line[i]=='/')&&(line[i+1]=='/')) {

>
> You can use find() here if you are using a std::string to get the
> position.
> std::string::size_type k = line.find("//");
>
> To see if it found it, compare it with std::string::npos.
>
> if ( k != std::string::npos )
> // ...
>
> Now, what you really want at this point is just to keep everything up to
> the //
> I'd just use substr
> line = substr( line, k );
> Now the // and everything past it are gone.
>
>> k=i-1;
>> remove(k, line);

>
> Ahh, did you really want to do this? What if you are in the middle of a
> block comment? Then you don't want to write this line. You should check
> to see about block comments before worrying about the line themselves.
> Move this section down below the block comment part.
>
>>
>> }
>> if ((line[i]=='/')&&(!begin)&&(line[i+1]=='*')) {
>> k2=i-1;
>> remove(k2, line);
>> begin=1;

>
> begin = 1
> Why not use C++'s bool?
> begin = true;
> But, of course, you never declared begin anywhere, did you?
> And begin isn't very descriptive of what this variable does either. So
> before while do something like:
> bool BlockComment = false;
>
> here you would do
> BlockComment = true;
>
>> }
>> if ((line[i]=='*')&&(begin)&&(line[i+1]=='/')){

>
> Again, with std::string this becomes
>
>> k3=i-1;
>> begin=0;
>> proceed(k3, line, strlen(line)) ;
>> }
>>
>> }
>> fp.close() ;
>> fpp.close() ;
>> return;
>> }
>>
>> }

>
> Hmm.. I dont' see where you checked for ending a block comment, or where
> you are not writing anything if you started a block comment.
>
> If you read thourgh the comments people provided and look at your code you
> should be able to figure it out. You have the logic about 50% right.
> Using chae pointers is making it much more difficult then it needs to be,
> std::strings will make it a whole lot easier, especially since then you
> can use find, substr and other functions.
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Begginer needs help Jake Alucard Ruby 2 09-26-2010 12:56 AM
begginer ASP question-plz help dchilkur ASP .Net 0 12-17-2007 10:23 PM
begginer question Micha³ S Cisco 5 04-19-2005 05:28 PM
Begginer Question about TK::filevent dhillon2004 Perl 0 09-03-2004 09:01 PM
Web Application begginer grzybek ASP .Net 2 11-24-2003 08:52 AM



Advertisments