# c++ problems



## clmlbx (Apr 24, 2008)

1. 
	
	



```
#include<fstream.h>
main()
{
ifstream in("file.txt");
ofstream out("fi.txt");
char item[20];
int count;
for(count=1;count<=164;count++)
{
in>>item;
out<<item<<" ";
in>>item;
out<<item<<" ";
in>>item;
out<<item<<" ";
}
in.close();
out.close();
return 0;
}
```
 
problem is ..... in above programme I have put condition " l<=164 ".........but I want to put condition till end of file .so when ever i use 
" in.eof()!=0 "  ...programme does not work.......... 

2. can there be a programme can give some type of processor error.......

what's wrong in this......



```
#include<fstream.h>
main()
{
int l;
char ch[25];
char c;
char name1[25];
char name2[25];
cout<<"Enter full address and file name wih extention ";
cout<<"\n\nEnter the infile name : ";
cin>>name1;
cout<<"\n\nEnter the outfile name : ";
cin>>name2;
ifstream in;
ofstream out;
in.open(name1);
out.open(name2);
for(l=1;l<=164;l++)
{
in.get(ch[l]);
c=ch[l];
switch(c)
{
case 'a':
ch[l]='e';
break;
case 'b':
ch[l]='o';
break;
case 'c':
ch[l]='f';
break;
case 'd':
ch[l]='p';
break;
case 'e':
ch[l]='g';
break;
case 'f':
ch[l]='q';
break;
case 'g':
ch[l]='h';
break;
case 'h':
ch[l]='r';
break;
case 'i':
ch[l]='i';
break;
case 'j':
ch[l]='s';
break;
case 'k':
ch[l]='j';
break;
case 'l':
ch[l]='t';
break;
case 'm':
ch[l]='a';
break;
case 'n':
ch[l]='u';
break;
case 'o':
ch[l]='b';
break;
case 'p':
ch[l]='v';
break;
case 'q':
ch[l]='c';
break;
case 'r':
ch[l]='w';
break;
case 's':
ch[l]='d';
break;
case 't':
ch[l]='x';
break;
case 'u':
ch[l]='k';
break;
case 'v':
ch[l]='y';
break;
case 'w':
ch[l]='l';
break;
case 'x':
ch[l]='z';
break;
case 'y':
ch[l]='m';
break;
case 'z':
ch[l]='n';
break;
case 'A':
ch[l]='E';
break;
case 'B':
ch[l]='O';
break;
case 'C':
ch[l]='F';
break;
case 'D':
ch[l]='P';
break;
case 'E':
ch[l]='G';
break;
case 'F':
ch[l]='Q';
break;
case 'G':
ch[l]='H';
break;
case 'H':
ch[l]='R';
break;
case 'I':
ch[l]='I';
break;
case 'J':
ch[l]='S';
break;
case 'K':
ch[l]='J';
break;
case 'L':
ch[l]='T';
break;
case 'M':
ch[l]='A';
break;
case 'N':
ch[l]='U';
break;
case 'O':
ch[l]='B';
break;
case 'P':
ch[l]='V';
break;
case 'Q':
ch[l]='C';
break;
case 'R':
ch[l]='W';
break;
case 'S':
ch[l]='D';
break;
case 'T':
ch[l]='X';
break;
case 'U':
ch[l]='K';
break;
case 'V':
ch[l]='Y';
break;
case 'W':
ch[l]='L';
break;
case 'X':
ch[l]='Z';
break;
case 'Y':
ch[l]='M';
break;
case 'Z':
ch[l]='N';
default:
goto cont;
}
cont:out.put(ch[l]);
}
in.close();
out.close();
return 0;
}
```


----------



## Sykora (Apr 25, 2008)

The preferred way of detecting whether a stream has run out on you in C++ is 


```
while (in >> something) {
// Something
}
```

And I really don't get what you're trying to do with the second piece of code.

Other nitpicking :

1. Indent the code. It isn't hard to do it, but it's very hard to read.
2. Use standard C++. It's better for all of us.


----------



## clmlbx (Apr 25, 2008)

^^ can u pls explain...........I am trying to use "eof()" so why it is not working...............(in first code)

in second code (time-pass).....I just jumbelled all the alphabets (both capital .......)and assigned them to each alphabet......means when u select a file then it will convert it into another file with that assigned jumbelled letters...(some type of code).in that problem was when ever i try to do it it gave me a cpu error.but that has been solved ............so i just need ..........

to use condition in both the code as "eof()"........in both if i use this condition then programme does not work......

and about the standard c++....I asked many questions about in this forum.but still I am not able to do that......means I just want to know what are changes in standard c++.........(tried some like ".h" is not used and some more")...but when ever I use to do in visual 2008 then it shows me a error .......it make me feels like i don't know a word about c++.......bcoz some simple programmes does not work in it.

so pls help


----------



## dheeraj_kumar (Apr 25, 2008)

Your second code opens file "in" and reads 164 characters, and its a very preliminary encryption algorithm, I think. It reads a character, and substitutes another character in place of it. What seems to be the problem with the code?

Edit: Just now saw the post, you can use something like this:

```
while(streamhandle)
{
//blah blah
}

example:

while(in)
{
//do all stuff
}
```

This would work as long the stream has data, and end of file has not been reached. 

And about your second question, what have you tried to do about standard c++ and what error do you receive? please be clear.


----------



## Sykora (Apr 25, 2008)

Checking for eof using eof() or feof() is a C practice. In C++, you use the state of the stream to tell you whether you're at the end :

eg the value of (cin >> x) is 0 if the cin didn't work, which is when the EOF character was sent. Otherwise, the value is non-zero.

Therefore, testing while (in >> x) will allow the loop to continue until the EOF is reached.

I don't know to what extent VS2008 is standards compliant. Maybe someone else can help out on that, but from what you say, it doesn't appear to be so.


----------



## clmlbx (Apr 26, 2008)

^^ can u be little more clear.........how to use state of the stream.

which compiler do you use if not vs 2008?


----------



## clmlbx (Apr 27, 2008)

ok guys, what do you use to clear the screen and to see the output(means when I want to test my programme,it runs and exit's   )like getch....so i can see the output

I am using visual 2008..............

I tried to use system("clear") but it gave error while running that "clear" is not a internal command or external command.....


----------



## QwertyManiac (Apr 27, 2008)

Use a simple cin>>some_variable; at the end. CLI programs are supposed to be run from the CMD/Shell actually...


----------



## clmlbx (Apr 27, 2008)

^^ how does state of the stream  work's ?

can some one try this I am getting errors that i don't know why...

errors:= 

 In function `int main()': 
 jump to label `decode' 
  from here 
  crosses initialization of `std:fstream out' 
  crosses initialization of `std::ifstream in' 

code:-



```
#include<fstream.h>
#include<iostream.h>
#include<process.h>
using namespace std;
int main()
{
int l;
char ch[25];
char c;
char name1[25];
char name2[25];
cout<<"\n\n 1. ENCRYPT";
cout<<"\n\n 2. DECRYPT";
cout<<"\n\n Enter your choice : ";
cin>>c;
switch(c)
{
case '1':
goto encode;
break;
case '2':
goto decode; 
break;       
}
encode:cout<<"Enter full address and file name wih extention ";
cout<<"\n\nEnter the file name to encrypt : ";
cin>>name1;
cout<<"\n\nEnter the file name to store data : ";
cin>>name2;
ifstream in;
ofstream out;
in.open(name1);
out.open(name2);
for(l=1;in.eof()==0;l++)
{
in.get(ch[l]);
c=ch[l];
switch(c)
{
case 'a':
ch[l]='e';
break;
case 'b':
ch[l]='o';
break;
case 'c':
ch[l]='f';
break;
case 'd':
ch[l]='p';
break;
case 'e':
ch[l]='g';
break;
case 'f':
ch[l]='q';
break;
case 'g':
ch[l]='h';
break;
case 'h':
ch[l]='r';
break;
case 'i':
ch[l]='i';
break;
case 'j':
ch[l]='s';
break;
case 'k':
ch[l]='j';
break;
case 'l':
ch[l]='t';
break;
case 'm':
ch[l]='a';
break;
case 'n':
ch[l]='u';
break;
case 'o':
ch[l]='b';
break;
case 'p':
ch[l]='v';
break;
case 'q':
ch[l]='c';
break;
case 'r':
ch[l]='w';
break;
case 's':
ch[l]='d';
break;
case 't':
ch[l]='x';
break;
case 'u':
ch[l]='k';
break;
case 'v':
ch[l]='y';
break;
case 'w':
ch[l]='l';
break;
case 'x':
ch[l]='z';
break;
case 'y':
ch[l]='m';
break;
case 'z':
ch[l]='n';
break;
case 'A':
ch[l]='E';
break;
case 'B':
ch[l]='O';
break;
case 'C':
ch[l]='F';
break;
case 'D':
ch[l]='P';
break;
case 'E':
ch[l]='G';
break;
case 'F':
ch[l]='Q';
break;
case 'G':
ch[l]='H';
break;
case 'H':
ch[l]='R';
break;
case 'I':
ch[l]='I';
break;
case 'J':
ch[l]='S';
break;
case 'K':
ch[l]='J';
break;
case 'L':
ch[l]='T';
break;
case 'M':
ch[l]='A';
break;
case 'N':
ch[l]='U';
break;
case 'O':
ch[l]='B';
break;
case 'P':
ch[l]='V';
break;
case 'Q':
ch[l]='C';
break;
case 'R':
ch[l]='W';
break;
case 'S':
ch[l]='D';
break;
case 'T':
ch[l]='X';
break;
case 'U':
ch[l]='K';
break;
case 'V':
ch[l]='Y';
break;
case 'W':
ch[l]='L';
break;
case 'X':
ch[l]='Z';
break;
case 'Y':
ch[l]='M';
break;
case 'Z':
ch[l]='N';
break;
case '0':
ch[l]='4';
break;
case '1':
ch[l]='5';
break;
case '2':
ch[l]='6';
break;
case '3':
ch[l]='3';
break;
case '4':
ch[l]='9';
break;
case '5':
ch[l]='1';
break;
case '6':
ch[l]='7';
break;
case '7':
ch[l]='0';
break;
case '8':
ch[l]='8';
break;
case '9':
ch[l]='2';
break;
default:
goto cont;
}
cont:out.put(ch[l]);
}
in.close();
out.close();
exit(5);
decode:cout<<"\n\nEnter full address and file name wih extention ";
cout<<"\n\nEnter the file name to decrypt : ";
cin>>name1;
cout<<"\n\nEnter the file name to store data : ";
cin>>name2;
ifstream rab;
ofstream raba;
rab.open(name1);
raba.open(name2);
for(l=1;in.eof()==0;l++)
{
rab.get(ch[l]);
c=ch[l];
switch(c)
{
case 'e':
ch[l]='a';
break;
case 'o':
ch[l]='b';
break;
case 'f':
ch[l]='c';
break;
case 'p':
ch[l]='d';
break;
case 'g':
ch[l]='e';
break;
case 'q':
ch[l]='f';
break;
case 'h':
ch[l]='g';
break;
case 'r':
ch[l]='h';
break;
case 'i':
ch[l]='i';
break;
case 's':
ch[l]='j';
break;
case 'j':
ch[l]='k';
break;
case 't':
ch[l]='l';
break;
case 'a':
ch[l]='m';
break;
case 'u':
ch[l]='n';
break;
case 'b':
ch[l]='o';
break;
case 'v':
ch[l]='p';
break;
case 'c':
ch[l]='q';
break;
case 'w':
ch[l]='r';
break;
case 'd':
ch[l]='s';
break;
case 'x':
ch[l]='t';
break;
case 'k':
ch[l]='u';
break;
case 'y':
ch[l]='v';
break;
case 'l':
ch[l]='w';
break;
case 'z':
ch[l]='x';
break;
case 'm':
ch[l]='y';
break;
case 'n':
ch[l]='z';
break;
case 'E':
ch[l]='A';
break;
case 'O':
ch[l]='B';
break;
case 'F':
ch[l]='C';
break;
case 'P':
ch[l]='D';
break;
case 'G':
ch[l]='E';
break;
case 'Q':
ch[l]='F';
break;
case 'H':
ch[l]='G';
break;
case 'R':
ch[l]='H';
break;
case 'I':
ch[l]='I';
break;
case 'S':
ch[l]='J';
break;
case 'J':
ch[l]='J';
break;
case 'T':
ch[l]='L';
break;
case 'A':
ch[l]='M';
break;
case 'U':
ch[l]='N';
break;
case 'B':
ch[l]='O';
break;
case 'V':
ch[l]='P';
break;
case 'C':
ch[l]='Q';
break;
case 'W':
ch[l]='R';
break;
case 'D':
ch[l]='S';
break;
case 'X':
ch[l]='T';
break;
case 'K':
ch[l]='U';
break;
case 'Y':
ch[l]='V';
break;
case 'L':
ch[l]='W';
break;
case 'Z':
ch[l]='X';
break;
case 'M':
ch[l]='Y';
break;
case 'N':
ch[l]='Z';
break;
case '4':
ch[l]='0';
break;
case '5':
ch[l]='1';
break;
case '6':
ch[l]='2';
break;
case '3':
ch[l]='3';
break;
case '9':
ch[l]='4';
break;
case '1':
ch[l]='5';
break;
case '7':
ch[l]='6';
break;
case '0':
ch[l]='7';
break;
case '8':
ch[l]='8';
break;
case '2':
ch[l]='9';
break;
default:
goto end;        
}
end:raba.put(ch[l]);
}
rab.close();
raba.close();
return 0;
}
```

using dev c++ for it


----------



## QwertyManiac (Apr 27, 2008)

Sykora's already explained in #2 how to use the state of a stream.

And why do you think people ask you to avoid using goto? You skipped the declarations of in and out fstream variables, they lie above the decode block and thus the error.


----------



## clmlbx (Apr 27, 2008)

ok then what can be used instead of goto ???? and there are still errors

there is one more problem in above code is that it print one extra char might be "\0"...........any sol.........


----------



## QwertyManiac (Apr 27, 2008)

goto can very well be replaced by using functions.

About other errors, I'll have to execute and check, later.


----------



## dheeraj_kumar (Apr 27, 2008)

Yes, goto is not recommended, as it performs an unconditional jump, meaning it can jump over important declarations/other statements so its not adviced to use goto. You can use functions and call them repeatedly to do whatever you need.


----------

