Like Tree1Likes
  • 1 Post By oogabooga

Help me with the do-while loop plz

This is a discussion on Help me with the do-while loop plz within the C++ Programming forums, part of the General Programming Boards category; My problem is in the while loop i.e from line # 32-56 Everything is working fine except until i input ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    82

    Help me with the do-while loop plz

    My problem is in the while loop i.e from line # 32-56

    Everything is working fine except until i input the filename that is present in that folder but not in ".txt" format.

    i want to receive error that file is not in ".txt"
    and if it is in.txt and not exist then a seperate error that file n/A.

    and to stop the loop if the file is in .txt format and exists there.. help me plz


    OK
    Name:  fine.JPG
Views: 109
Size:  47.1 KB

    Error:
    Name:  error.JPG
Views: 119
Size:  50.3 KB


    CODE:
    Code:
    #include <iostream>
    #include <fstream>
    #include <stdlib.h>
    #include <string>
    
    using namespace std;
    
    // Function protos
    void init_array(char ascii[63], char myb[64]);
    
    // !! Function protos
    
    int main()
    {
    //declaration & init
        char ascii[63];
        char myb[64];
        string filename;
    
        int a=48;
        int b=0;
        int count_filename=0;
        int count_orig=0;
        int chck_name=0;
        fstream in_orig;
        //ofstream test; // for Testing
    // !! Declaration
    // Input fiile name and open
        //test.open("test.txt");
        init_array(ascii,myb);    // value assigned to array
    //    test << ascii;
        do
        {
            cout << "Enter a file name or complete path or 0 to exit:" << endl;
            getline(cin,filename);
            in_orig.open(filename.c_str());
            if (filename == "0")
            {
                exit(1);
            }
    
    
            else if (filename.find(".txt") != string::npos)
            {
    
                if (in_orig.fail())
                {
                    cout << "-File doesnot exist at the current location.\n"<< endl;
                }
            }
    
          else
          cout << "-Only \".txt\" files are suported\n\n";
    
        }
        while ((in_orig.fail()) || (filename.find(".txt") == string::npos));
    // !! filename and open
    
    
        return 0;
    }
    
    
    // Functions to Initialize arrays
    void init_array(char ascii[63], char myb[64])
    {
        // ofstream test1;
        //test1.open("test1.txt");
        int a=48;
        int b=0;
    
        /* storing values in ascii, i.e
        ={0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z, }*/
        a=48;
        while (a < 123)
        {
            if (((a >= 48) && (a < 58)) || ((a >= 65) && (a <= 90)) || ((a >= 97) && (a <= 122)))
            {
                ascii[b]=a;
                b++;
            }
            if ( b==63)
            {
                ascii[b]=32;
            }
            a++;
    
    
        }
    // !! Stored in ascii
        //cout  << ascii << endl;
        a=0;
        while (a < 63)
        {
            myb[a]=char(a);
            /*char c;
            c=myb[a]| (char)65;
    
            //test1 << a << " " << (int)myb[a] << endl;*/
            (int)a++;
        }
        /*char c=myb[1];
        c = c << 6;
        c= c| char(1);
         test1 << c << endl;
        */
    }

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Maybe something like this:
    Code:
        while (true)
        {
            cout << "Enter a file name or complete path or 0 to exit:" << endl;
            getline(cin,filename);
            if (filename == "0")
                exit(1);
            else if (filename.find(".txt") != filename.size() - 4)
                cout << "-Only \".txt\" files are suported\n\n";
            else
            {
                in_orig.open(filename.c_str());
                if (!in)
                    cout << "-File doesnot exist at the current location.\n"<< endl;
                else
                    break; // exit loop
            }
        }
    Last edited by oogabooga; 02-22-2012 at 03:05 PM.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    82
    yeah !! thnx alot mate

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-26-2011, 06:36 PM
  2. Replies: 23
    Last Post: 04-05-2011, 03:40 PM
  3. Help.. newbie for loop..stuck with the loop..
    By jochen in forum C Programming
    Replies: 15
    Last Post: 10-01-2007, 12:31 AM
  4. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 01:46 AM
  5. While loop ..crating an infanat loop
    By fmchrist in forum C Programming
    Replies: 7
    Last Post: 01-14-2006, 09:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21