fstream with switch case

B

begum

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;
}
 
R

red floyd

begum said:
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?
 
B

begum

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 said:
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?
 
R

red floyd

begum said:
> #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.
 
M

mlimber

begum said:
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/containers.html#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 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top