Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 24-04-2008, 10:58 PM   #1 (permalink)
Wise Old Owl
 
clmlbx's Avatar
 
Join Date: Aug 2006
Location: Indore
Posts: 1,690
Question c++ problems


1.
Code:
#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......


Code:
#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;
}
__________________
Athlon II X4 635 @ 2.9Ghz   Gigabyte GA-MA785GMT-US2H   Kingston 2x2 Gb 1333Mhz DDR3   WDC 500Gb Green   Palit GTS 250 512mb   Tagan 500W   Samsung B2030   Lg DVD Writer
clmlbx is online now  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 25-04-2008, 09:50 AM   #2 (permalink)
I see right through you.
 
Sykora's Avatar
 
Join Date: Sep 2005
Location: Chennai
Posts: 597
Default Re: c++ problems

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

Code:
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.
__________________
I didn't make the world, I only try to live in it.
http://lucentbeing.com
-- Sykora --
Sykora is offline  
Old 25-04-2008, 05:35 PM   #3 (permalink)
Wise Old Owl
 
clmlbx's Avatar
 
Join Date: Aug 2006
Location: Indore
Posts: 1,690
Default Re: c++ problems

^^ 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
__________________
Athlon II X4 635 @ 2.9Ghz   Gigabyte GA-MA785GMT-US2H   Kingston 2x2 Gb 1333Mhz DDR3   WDC 500Gb Green   Palit GTS 250 512mb   Tagan 500W   Samsung B2030   Lg DVD Writer
clmlbx is online now  
Old 25-04-2008, 05:36 PM   #4 (permalink)
Legen-wait for it-dary!
 
dheeraj_kumar's Avatar
 
Join Date: Dec 2004
Location: Chennai
Posts: 2,471
Default Re: c++ problems

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:
Code:
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.
__________________
If the Start Windows Restart when Windows starts check box is checked Windows Restart will start automatically every time Windows is started. - Actual excerpt from a windows program help file
dheeraj_kumar is offline  
Old 25-04-2008, 06:21 PM   #5 (permalink)
I see right through you.
 
Sykora's Avatar
 
Join Date: Sep 2005
Location: Chennai
Posts: 597
Default Re: c++ problems

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.
__________________
I didn't make the world, I only try to live in it.
http://lucentbeing.com
-- Sykora --
Sykora is offline  
Old 26-04-2008, 12:22 AM   #6 (permalink)
Wise Old Owl
 
clmlbx's Avatar
 
Join Date: Aug 2006
Location: Indore
Posts: 1,690
Default Re: c++ problems

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

which compiler do you use if not vs 2008?
__________________
Athlon II X4 635 @ 2.9Ghz   Gigabyte GA-MA785GMT-US2H   Kingston 2x2 Gb 1333Mhz DDR3   WDC 500Gb Green   Palit GTS 250 512mb   Tagan 500W   Samsung B2030   Lg DVD Writer
clmlbx is online now  
Old 27-04-2008, 01:43 AM   #7 (permalink)
Wise Old Owl
 
clmlbx's Avatar
 
Join Date: Aug 2006
Location: Indore
Posts: 1,690
Default Re: c++ problems

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.....
__________________
Athlon II X4 635 @ 2.9Ghz   Gigabyte GA-MA785GMT-US2H   Kingston 2x2 Gb 1333Mhz DDR3   WDC 500Gb Green   Palit GTS 250 512mb   Tagan 500W   Samsung B2030   Lg DVD Writer
clmlbx is online now  
Old 27-04-2008, 01:58 AM   #8 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: c++ problems

Use a simple cin>>some_variable; at the end. CLI programs are supposed to be run from the CMD/Shell actually...
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Old 27-04-2008, 02:24 AM   #9 (permalink)
Wise Old Owl
 
clmlbx's Avatar
 
Join Date: Aug 2006
Location: Indore
Posts: 1,690
Unhappy Re: c++ problems

^^ 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:-


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
__________________
Athlon II X4 635 @ 2.9Ghz   Gigabyte GA-MA785GMT-US2H   Kingston 2x2 Gb 1333Mhz DDR3   WDC 500Gb Green   Palit GTS 250 512mb   Tagan 500W   Samsung B2030   Lg DVD Writer

Last edited by clmlbx; 27-04-2008 at 02:31 AM.
clmlbx is online now  
Old 27-04-2008, 02:36 AM   #10 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: c++ problems

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.
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Old 27-04-2008, 02:41 AM   #11 (permalink)
Wise Old Owl
 
clmlbx's Avatar
 
Join Date: Aug 2006
Location: Indore
Posts: 1,690
Default Re: c++ problems

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.........
__________________
Athlon II X4 635 @ 2.9Ghz   Gigabyte GA-MA785GMT-US2H   Kingston 2x2 Gb 1333Mhz DDR3   WDC 500Gb Green   Palit GTS 250 512mb   Tagan 500W   Samsung B2030   Lg DVD Writer

Last edited by clmlbx; 27-04-2008 at 02:48 AM.
clmlbx is online now  
Old 27-04-2008, 03:13 AM   #12 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: c++ problems

goto can very well be replaced by using functions.

About other errors, I'll have to execute and check, later.
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Old 27-04-2008, 11:27 AM   #13 (permalink)
Legen-wait for it-dary!
 
dheeraj_kumar's Avatar
 
Join Date: Dec 2004
Location: Chennai
Posts: 2,471
Default Re: c++ problems

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.
__________________
If the Start Windows Restart when Windows starts check box is checked Windows Restart will start automatically every time Windows is started. - Actual excerpt from a windows program help file
dheeraj_kumar is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

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 On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Post ur mobile internet queries, problems n problems here sourishzzz1234 Buying Advice 2 19-01-2008 07:34 PM
Problems and more problems with Linux phreak0ut Open Source 25 16-11-2006 09:38 PM
OS problems .......... please help!!! Mohnishgs Software Q&A 4 14-05-2006 12:24 AM
XP HDD problems talrejaharish Software Q&A 3 29-03-2005 11:25 AM
A lot of problems vasanth_12345 Open Source 5 20-03-2005 12:31 PM

 
Latest Threads
- by Sujeet
- by clmlbx
- by Sujeet
- by icebags

Advertisement




All times are GMT +5.5. The time now is 10:58 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2