Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How should I test my program on the following input strings?

1 Answer

+1 vote
answered Sep 27, 2022 by Peter Minarik (84,720 points)
When you run your application in OnlineGDB, at the bottom you can specify the command line arguments and can switch between Interactive Console and Text for standard input.

Select Text, and copy your test content there.

Assuming you'll need to read your test data from the standard input...

If not, read it from a file and put your test data in a file.

If you share some code, more specific help could be provided. :)

Good luck!
commented Sep 27, 2022 by Nirali Patel (750 points)
okay, then here is the code:
#include <iostream>
#include <fstream>
#include <string>
#include <array>
#define max_DFA_states 10


using namespace std;

   //checks if the user wants to enter a string
int user_choice(char user_input, int max_n)
{
    string str;
    fstream newfile;
    
 //checks if the user wants to enter a string
    for(int i = 0; i < max_n; i++)
    {
    cout<<"Do you want to enter a string?"<<endl;
    cin>> user_input;
    
    //if the user enters y, it asks to enter a string over the alphabet
    if(user_input == 'y')
    {
        cout<<"Please enter a string over a alphabet:"<<endl;
        cin>> str;
    }
    
    //if the user enters n, the program terminates
    if(user_input == 'n')
    {
        cout<<"Your done"<<endl;
        return 0;
    }
    
    //opens the file to read the string
        newfile.open("./in.txt", ios::in);
    //checks whether file is open
        for(int i = 0; i < str.size(); i++)
        {
        if (newfile. is_open())
        {
            string str;
            //reads data from file object and puts it into string
            while (getline(newfile, str))
            {
                //prints the data of the string
                cout<< str << "\n";
            }
            //closes the file object
            newfile.close();
        }
        }
    
    }
    return 0;
}
    
    int DFA_implement()
    {
        //DFA implementation
        //begins with the start state of DFA
        int start_state = 1;
        string ch[36] = {'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',
        0,1,2,3,4,5,6,7,8,9};
        
        string s1[36] = {'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'};
        
        string s2[26] = {".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"};
        
        string s3 = {".com"};
        
        int current_state;
        char final_string;
        int final_state = 10;
        int previous_state;
        int num_DFA_states = 10;
        //prints the start state
        cout<<"The start state is: "<< start_state << endl;
        
        //After string is processed on dfa, print the character and name of the current state of dfa
        for(int i = 0; i < num_DFA_states; i++)
        {
            previous_state += current_state;
            current_state += ch[36];
            cout<<"The string is: "<< ch[36] << endl;
            cout<<"The name of the current state of dfa is: "<< current_state <<endl;
    
    
        //check if the character reaches end of the string
        if(previous_state == final_string)
        {
            cout<<"stop, the final string is: "<< final_string << endl;
        }
        }
        return 0;
    }
    
    //check whether string is accepted or rejected
    int check_string(string s)
    {
        char user_input;
        int max_n;
        int ch;
        int final_state = 10;
        int previous_state;
        
        for(int i = 0; i < max_n; i++)
        {
            if((previous_state == 0) || (previous_state == 1))
            {
                previous_state = 0;
                final_state += 1;
            }
            
            
        }
            previous_state = final_state;
    
    //if final state is reached, check whether string is accepted or rejected
        if (final_state == 10)
        {
            cout<<"String is accepted"<<endl;
        }
        else
        {
            cout<<"string is not accepted"<<endl;
        }
    //Ask the user again if they want to enter a string
    //function call to user user_choice
     return 0;   
    }



int main()
{
    //Prints project 1, section number, semester, written by, & Instructor: Marvin Nakayama, [email protected]
    cout<<"Project 1 "<<endl;
   
    
    int max_n;
    char user_input;
    string s;
    
    //function calls
    user_choice(user_input, max_n);
    DFA_implement();
    check_string(s);
    
    
}
commented Sep 28, 2022 by Peter Minarik (84,720 points)
Your code does not compile, it's hard to understand what you're trying to do.

This is your homework, so I won't write it for you, but I'll point out problems with it in the comments. :)

#include <iostream>
#include <fstream>
#include <string>
#include <array>

#define max_DFA_states 10 // Unused macro. --> USE THIS MACRO INSTEAD OF CREATING final_state?

using namespace std;

int user_choice(char user_input, int max_n) // user_input is never read. --> DO NOT PASS IT IN AS AN ARGUMENT BUT DECLARE IT AS A LOCAL VARIABLE
{
    string str;
    fstream newfile;
    
    for (int i = 0; i < max_n; i++)
    {
        cout << "Do you want to enter a string?" << endl;
        cin >> user_input;
    
        if (user_input == 'y')
        {
            cout << "Please enter a string over a alphabet:" << endl;
            cin >> str;
        }
    
        if (user_input == 'n')
        {
            cout<<"Your done"<<endl;
            return 0;
        }
        
        // What if the input wasn't either 'n' or 'y'? --> HANDLE UNEXPECTED USER INPUTS
    
        newfile.open("./in.txt", ios::in); // Does this file exist? --> ADD THIS FILE TO YOUR PROJECT OR ENSURE WITH DIFFERENT MEANS THAT THE FILE EXISTS
        for (int i = 0; i < str.size(); i++) // Why do you read as many lines from the file as many characters the user entered last time (which was either 'y' or 'n' under normal circumstances)?! --> REVIEW YOUR LOGIC
        {
            if (newfile.is_open()) // You should do this outside of the loop. --> MOVE THIS CHECK BEFORE THE LOOP
            {
                string str;
                while (getline(newfile, str))
                {
                    cout << str << "\n";
                }
                newfile.close(); // You close the file in the loop but you're going to read from the file in the next loop iteration. --> MOVE THIS AFTER THE LOOP FINISHES
            }
        }
    
    }
    return 0; // No point having a return value if it's always 0. --> REMOVE THE STATEMENT AND CHANGE THE RETURN TYPE OF YOUR FUNCTION TO void
}
    
int DFA_implement()
{
    int start_state = 1;
    string ch[36] = {'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', 0,1,2,3,4,5,6,7,8,9}; // Type mismatch: you're putting characters and integers into a string array. --> STORE STRINGS IN A STRING[] OR REVIEW YOUR DATA TYPE
    
    string s1[36] = {'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'}; // Type mismatch: you're putting characters in a string array. The array length does not match the number of items passed in. --> USE CHAR[] FOR YOUR DATA TYPE. PASS IN THE RIGHT AMOUNT OF ELEMENTS OR SET THE RIGHT SIZE OR LET THE COMPILER FIGURE OUT THE SIZE (char[] myChars = { 'a', 'b', ... })
    
    string s2[26] = {".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"};
    
    string s3 = {".com"};
    
    int current_state; // Uninitialized variable (the value is not determined) --> SET A VALUE
    char final_string; // Misleading name (it is a single character, not a string). Uninitialized variable (the value is not determined) --> REVIEW THE NAME. SET A VALUE BEFORE READING IT `if(previous_state == final_string)`
    int final_state = 10;
    int previous_state; // Uninitialized variable (the value is not determined). --> SET THE INITIAL VALUE
    int num_DFA_states = 10;
    cout<<"The start state is: "<< start_state << endl;
    
    for(int i = 0; i < num_DFA_states; i++)
    {
        previous_state += current_state;
        current_state += ch[36]; // Index out of bounds. ch is 36 character long [0..35]. 35 is the last element, hence 36 is not inside the array. Are you sure you want to increment a number by a character (code)? --> REVIEW YOUR LOGIC
        cout<<"The string is: "<< ch[36] << endl; // Index out of bounds. ch is 36 character long [0..35]. 35 is the last element, hence 36 is not inside the array. --> REVIEW YOUR LOGIC
        cout<<"The name of the current state of dfa is: "<< current_state <<endl;

        if (previous_state == final_string)
        {
            cout << "stop, the final string is: " << final_string << endl;
        }
    }
    return 0; // No point having a return value if it's always 0. --> REMOVE THE STATEMENT AND CHANGE THE RETURN TYPE OF YOUR FUNCTION TO void
}
    
int check_string(string s) // Unused argument --> REMOVE IT
{
    char user_input; // Uninitialized variable (the value is not determined). Unused variable. --> REMOVE IT
    int max_n; // Uninitialized variable (the value is not determined) --> PASS IT IN IN THE PARAMETER LIST
    int ch; // Uninitialized variable (the value is not determined). Unused variable. --> REMOVE IT
    int final_state = 10;
    int previous_state; // Uninitialized variable (the value is not determined). --> SET THE INITIAL VALUE
    
    for (int i = 0; i < max_n; i++) // Why do you do this max_n times? Nothing changes in the loop, just final_state, so you could have just written final_state += max_n instead of having this whole loop. --> REVIEW YOUR LOGIC
    {
        if ((previous_state == 0) || (previous_state == 1))
        {
            previous_state = 0;
            final_state += 1;
        }
    }
    previous_state = final_state; // Noone will ever read previous_state. No need to change its value. --> REMOVE THE ASSIGNMENT OR REVIEW YOUR LOGIC.

    if (final_state == 10)
    {
        cout<<"String is accepted"<<endl;
    }
    else
    {
        cout<<"string is not accepted"<<endl;
    }

    return 0; // No point having a return value if it's always 0. --> REMOVE THE STATEMENT AND CHANGE THE RETURN TYPE OF YOUR FUNCTION TO void
}

int main()
{
    cout<<"Project 1 "<<endl;

    int max_n;       // Uninitialized variable (the value is not determined) --> SET A VALUE
    char user_input; // Uninitialized variable (the value is not determined). Unused variable (user_choice() never uses the first argument passed in). --> REMOVE IT
    string s;        // Uninitialized variable  the value is not determined). Unused variable (check_string() never uses the argument passed in)--> REMOVE IT
    
    //function calls
    user_choice(user_input, max_n);
    DFA_implement();
    check_string(s);
}



I have no idea what DFA is. So I cannot give you advise on how your program should work. I don't understand the logic you're doing there. Please, review what your code is supposed to do.


*** WHERE TO PUT THE INPUTS? ***

Now, that we're done with the review, I'd figure the following
- All your 'y' and 'n' should come from the standard input (TEXT, instead of interactive console, see my original answer)
- All the e-mail addresses should be stored in your in.txt file, that your program reads.

I hope it's much clearer now.

Good luck!
commented Sep 29, 2022 by Nirali Patel (750 points)
Okay, I have made the changes based on the comments. But, can I please get an example for how and where to input strings. I have all the email addresses stored in in.txt file.
commented Sep 29, 2022 by Peter Minarik (84,720 points)
E-mail addresses stored in a file sounds good.

The "how and where to input strings" confuses me. At the bottom of your page, below the code, you should have a terminal window where all the output is printed and you enter inputs there too.

Are you asking how to use OnlineGDB, how the user interface works, or are you asking how to write a C++ program that asks the user to input some text (your code already has this: `cin >> str;`).

Does the following little code work for you? Can you see the messages and can you enter your name?

#include <iostream>
#include <string>

int main()
{
    std::string name;
    std::cout << "What's your name? ";
    std::getline(std::cin, name);
    std::cout << "It's nice to meet you, " << name << "!" << std::endl;

    return 0;
}
commented Oct 1, 2022 by Nirali Patel (750 points)
Yes this little code works for me, but I am not sure what you mean by I can see the messages? And I have to test my program on the email addresses. The very first answer that you gave, is that the correct way to do it? And what are the command line arguments?
commented Oct 1, 2022 by Peter Minarik (84,720 points)
edited Oct 1, 2022 by Peter Minarik
Command line arguments are arguments you provide to your executable.

E.g.:

C:\Work\MyAppTest>MyApplication.exe argument1 argument2

The above line means you launch MyApplication.exe program, and the command line arguments you provide for it are `argument1` and `argument2`.

But as we've established, you already put the e-mail addresses in a file (and your code reads files fine `getline(newfile, str)`).

And on the standard input, you should keep reading the 'y' and 'n' characters. This is done by the user_input() function `cin >> user_input;`.

Therefore, you don't need to use command line arguments for this exercise.

So you have your e-mail file and you need to provide the 'y'/'n' the same way as the above little example you provided the name (bottom of the screen, Standard Input). Except you should change it from "Interactive Console" to "Text".

Please, see this image: https://i.imgur.com/U3R9cd6.png

I hope this helps.
commented Oct 1, 2022 by Nirali Patel (750 points)
I tried with the text, and run it but I think its going infinite times and taking too long. And its not testing the way it should. Can you please help me fix this. Thank you.
https://www.onlinegdb.com/edit/HJbPWj4cL
commented Oct 2, 2022 by Peter Minarik (84,720 points)
You can select "Interactive Console" instead of "Text" for the Standard Input, but then you'll have to enter all the 'y' and 'n' manually (which I suppose wasn't the preferred way considering you've got 20 of them).

The link you shared does not work. You probably just copied it from the browser. You've got to save your project (SAVE button) and share the link with the SHARE button.

You can also add some debug messages to track what your code is doing (e.g.: `std::cout << "[DFA_implement] i = " << i << std::endl;` could be used to keep an eye on your loop variable in DFA_implement())
commented Oct 2, 2022 by Nirali Patel (750 points)
edited Oct 2, 2022 by Nirali Patel
okay, I am not sure what is wrong in my code? When I input n, it still reads the strings and doesn't stop. And when I input y, it only reads the first character of email addresses. And it doesn't even execute the DFA implementation. Can you please help me?
Thank you.
Here is the code:
https://onlinegdb.com/P-7qGX-dj
commented Oct 2, 2022 by Peter Minarik (84,720 points)
"When I input n, it still reads the strings and doesn't stop."

Yes, it is because this code:

     //if the user enters n, program terminates
    if(user_input == 'n')
    {
        cout<<"Your done"<<endl;
    }

There is no termination here (opposed to the comment). You just print something. You do not stop the execution of the function (return).

Also, after the user enters 'y', you keep reading the standard input, while you're supposed to read a single line from the file.

Then ask if they want more (y/n). Then read the next line.

Instead, you read the whole file in one go.

Review your logic and check what your code does and what the task says you're supposed to do.

Do it via the Interactive Console. When it works fine, you can switch back to TEXT to automatically run everything, if you desire so.
commented Oct 3, 2022 by Nirali Patel (750 points)
Yes but that's in the void function, then how should I return or terminate the program? How should I read line by line from the file? And is it fine if I share the project question with you, if you can better understand and tell me what its asking for?
commented Oct 3, 2022 by Peter Minarik (84,720 points)
"how should I return or terminate the program?"

There is an exit() command, where you can also provide an error code.

E.g.: `exit(0);` would terminate the program.

See https://cplusplus.com/reference/cstdlib/exit/ for more details.

"How should I read line by line from the file?"

That's kind of your task. :)

You can open the file and only call `getline(newfile, str)` when the user entered 'y'.
commented Oct 4, 2022 by Nirali Patel (750 points)
How can I get the character of the array{0,1,2,3,4,5,6,7,8,9}.This should also print the character . I tried doing like array[]++, but that took up to something else only. How can I print the rest of the characters with the numbers. Please help me. Thank you.
Here is the code:
https://onlinegdb.com/PX57Lx1hi
commented Oct 4, 2022 by Peter Minarik (84,720 points)
I think you could benefit from doing some tutorials.

I highly recommend doing some online search for some training. Check this out on basic data types: https://www.w3schools.com/cpp/cpp_data_types.asp

If you want to store characters, not integral numbers in an array, then do not use numeric literals, but character literals.

E.g.:

char digits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

Again, do some online tutorials and learn the language basics:

https://cplusplus.com/doc/tutorial/
https://www.w3schools.com/cpp/
https://www.tutorialspoint.com/cplusplus/index.htm
https://www.learncpp.com/

Pick one of the above or find any alternatives and do the trainings so you'd be familiar with programming and C++ in specific as well.

Good luck! :)
commented Oct 5, 2022 by Nirali Patel (750 points)
okay, thank you for the help
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...