fstream with switch case

Discussion in 'C++' started by begum, Dec 12, 2006.

  1. begum

    begum Guest

    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;
    }
     
    begum, Dec 12, 2006
    #1
    1. Advertising

  2. begum

    red floyd Guest

    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?
     
    red floyd, Dec 12, 2006
    #2
    1. Advertising

  3. begum

    begum Guest

    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?
     
    begum, Dec 12, 2006
    #3
  4. begum

    red floyd Guest

    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.
     
    red floyd, Dec 12, 2006
    #4
  5. begum

    mlimber Guest

    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/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 < 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
     
    mlimber, Dec 12, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. gane kol

    switch case with enum

    gane kol, May 6, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    7,311
    Kevin Spencer
    May 6, 2005
  2. Brian J. Sayatovic

    Why constant ints in switch case expressions?

    Brian J. Sayatovic, Jul 2, 2003, in forum: Java
    Replies:
    22
    Views:
    21,857
    Dale King
    Jul 9, 2003
  3. vic

    switch case statement

    vic, Mar 3, 2004, in forum: Java
    Replies:
    9
    Views:
    5,850
  4. Armando
    Replies:
    6
    Views:
    747
    Armando
    Jan 29, 2004
  5. Switch Within A Switch

    , Apr 22, 2006, in forum: Javascript
    Replies:
    7
    Views:
    110
    Lasse Reichstein Nielsen
    Apr 22, 2006
Loading...

Share This Page