Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > fstream with switch case

Reply
Thread Tools

fstream with switch case

 
 
begum
Guest
Posts: n/a
 
      12-12-2006
in my program ý want to use a help menu and i want to connect it with
a .txt file. in case 4 ý want to use fstream. how can i do it?
thank you.


#include <iostream>
#include<string.h>

using namespace std;

int main()
{ char lang;
int secim;
int n;
int pid=1||2||3||4||5;
int tid=1||2;
int amount;

cout<<"* INVENTORY MANAGEMENT SYSTEM *"<<endl;
cout<<"* [1] Product operations *"<<endl;
cout<<"* [2] Depot operations *"<<endl;
cout<<"* [3] Stock maintanence *"<<endl;
cout<<"* [4] Help *"<<endl;
cout<<"* [5] Exit *"<<endl;

cout<<"***********************************"<<endl;
cout<<"Enter your choice from above MENU:"<<endl;
cin>>secim;

switch(secim){
case 3:

cout<<"* Inventory management system *"<<endl;
cout<<"***********************************"<<endl;
cout<<"* STOCK MAiNTANENCE *"<<endl;
cout<<"Enter depotid="<<endl;
cin>>tid;
if(tid!=1||2)
cout<<"\a\a\a invalid depotid,please enter correct id"<<endl;
cin>>tid;

cout<<"Enter productid="<<endl;
cin>>pid;
if(pid!=1||2||3||3||4||5)
cout<<"\a\a\a invalid productid,please enter correct product
id"<<endl;
cin>>pid;

cout<<"Enter transaction amount="<<endl;
cin>>amount;

break;



case 5:

cout<<"Are you sure quit or continue"<<endl;
cin>> lang;
if(lang=='q'||lang=='Q')
break;
system("cls");

if(lang=='c'||lang=='C')
system("start");







}
return 0;
}

 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      12-12-2006
begum wrote:
> in my program ý want to use a help menu and i want to connect it with
> a .txt file. in case 4 ý want to use fstream. how can i do it?
> thank you.
>
> [redacted]


#include <fstream>

Seriously, though, what do you want to do with your txt file?
 
Reply With Quote
 
 
 
 
begum
Guest
Posts: n/a
 
      12-12-2006
i want to read txt file when i enter 4 in my program. and my big
problem is that what am i going to write in case 4. i did'nT write
anything.


red floyd yazdı:
> begum wrote:
> > in my program ý want to use a help menu and i want to connect it with
> > a .txt file. in case 4 ý want to use fstream. how can i do it?
> > thank you.
> >
> > [redacted]

>
> #include <fstream>
>
> Seriously, though, what do you want to do with your txt file?


 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      12-12-2006
begum wrote:
> #include <iostream>
> #include <string.h>

Delete second include. It's unused.

> [redacted]
>
> int pid=1||2||3||4||5;

This will set pid equal to 1. What are you trying to do here?
> int tid=1||2;

This will set tid equal to 1. What are you trying to do here?

> [redacted]
> switch(secim){
> case 3:
>
> cout<<"* Inventory management system *"<<endl;
> cout<<"***********************************"<<endl;
> cout<<"* STOCK MAiNTANENCE *"<<endl;
> cout<<"Enter depotid="<<endl;
> cin>>tid;
> if(tid!=1||2)

This will always return true. It's equivalent to ((tid != 1) || 2 ). 2
is always true. Please read your textbook on boolean expressions.
> cout<<"\a\a\a invalid depotid,please enter correct id"<<endl;
> cin>>tid;
>
> cout<<"Enter productid="<<endl;
> cin>>pid;
> if(pid!=1||2||3||3||4||5)

See comment above for tid.

> cout<<"\a\a\a invalid productid,please enter correct product
> id"<<endl;
> cin>>pid;
>
> cout<<"Enter transaction amount="<<endl;
> cin>>amount;
>
> break;
>
>
>
> case 5:
>
> cout<<"Are you sure quit or continue"<<endl;
> cin>> lang;
> if(lang=='q'||lang=='Q')
> break;
> system("cls");
>
> if(lang=='c'||lang=='C')
> system("start");


I don't think this line does what you think it does.
> [redacted]


I really think you need to go back to your textbook and learn some basic
syntax.
 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      12-12-2006
begum wrote:
> in my program ý want to use a help menu and i want to connect it with
> a .txt file. in case 4 ý want to use fstream. how can i do it?
> thank you.
>
>
> #include <iostream>
> #include<string.h>


You don't use this header. Did you mean <string> or <cstring> (I'd hope
the former; see
http://parashift.com/c++-faq-lite/co...tml#faq-34.1)?

> using namespace std;
>
> int main()
> { char lang;
> int secim;
> int n;
> int pid=1||2||3||4||5;
> int tid=1||2;
> int amount;


This isn't C -- there's no need to declare all your variables up front.
Declare them only when you need them and can initialize them.

>
> cout<<"* INVENTORY MANAGEMENT SYSTEM *"<<endl;
> cout<<"* [1] Product operations *"<<endl;
> cout<<"* [2] Depot operations *"<<endl;
> cout<<"* [3] Stock maintanence *"<<endl;
> cout<<"* [4] Help *"<<endl;
> cout<<"* [5] Exit *"<<endl;
>
> cout<<"***********************************"<<endl;
> cout<<"Enter your choice from above MENU:"<<endl;


For instance, move "int secim;" here.

> cin>>secim;
>
> switch(secim){
> case 3:
>
> cout<<"* Inventory management system *"<<endl;
> cout<<"***********************************"<<endl;
> cout<<"* STOCK MAiNTANENCE *"<<endl;
> cout<<"Enter depotid="<<endl;
> cin>>tid;
> if(tid!=1||2)
> cout<<"\a\a\a invalid depotid,please enter correct id"<<endl;
> cin>>tid;
>
> cout<<"Enter productid="<<endl;
> cin>>pid;
> if(pid!=1||2||3||3||4||5)


This is legal but won't do what you want. Try

if(pid!=1 && pid!=2 && pid!= 3 && pid !=4 || pid != 5)

or better:

if( pid < 1 || pid > 5 )

> cout<<"\a\a\a invalid productid,please enter correct product


Ok, but after this print, you need to ask the user to enter it again,
restart the loop (lookup the continue keyword), exit, or something
other than simply proceeding onward as though there was no error.

> id"<<endl;
> cin>>pid;
>
> cout<<"Enter transaction amount="<<endl;
> cin>>amount;
>
> break;
>
>
>
> case 5:
>
> cout<<"Are you sure quit or continue"<<endl;
> cin>> lang;
> if(lang=='q'||lang=='Q')
> break;
> system("cls");
>
> if(lang=='c'||lang=='C')
> system("start");
> }
> return 0;
> }


If you just want to dump a text file to the screen, you could do:

ifstream file( "help.txt" );
string s;
while( getline( file, s ) )
{
cout << s << '\n';
}

There are other, more efficient ways to do it, but this way is probably
the more understandable for you. Alternately, just hard code it (either
in a constant or as an inline constant), e.g.,

cout << "Help text goes here\n"
"The next line goes here\n"
"The last line goes here\n";

Cheers! --M

 
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
lower case to upper case Janice C Programming 17 12-14-2004 02:35 PM
how to case select with case-insensitive string ? Tee ASP .Net 3 06-23-2004 07:40 PM
Possible to turn on/off cookieless sessions dynamically on a case by case basis at run-time? Steve Franks ASP .Net 2 06-10-2004 02:04 PM
Scorsese Collection: Keep case vs Snap case Ray DVD Video 0 05-30-2004 04:04 AM
what is different between <fstream.h> and <fstream>MS VC++ Armando C++ 6 01-29-2004 09:01 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57