Hello siabapet,
You should have compiled you code before posting it. Then you could aks about any errors that you do not understand.
If your objective is to write your code for the compiler this shloud shave a few milliseconds off the compile time.
1 2
|
#include <iostream>
using namespace std; char A[][2000]{ "Dave buy 2 mangoes", "Felix buys 1 basketball", "Sara sells shoes for 200 dollars" }; string x; int i; main() { cout << "Enter your sentence : "; cin >> x; for (i = 0; i < 1; i++) { if (x == A[i]) { cout << "The sentence" << x << "Founded"; } else { cout << "The sentence" << x << "Not Found"; } }return 0; }
|
Oh BTW there are several errors in line 2.
If you would do something more like this:
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
|
#include <iostream>
using namespace std;
main()
{
char A[][2000]{ "Dave buy 2 mangoes", "Felix buys 1 basketball", "Sara sells shoes for 200 dollars" };
string x; // <--- Should have a better name.
//int i; // <--- Should be defined in the for loop.
cout << "Enter your sentence : ";
std::getline(std::cin, x);
for (int i = 0; i < 1; i++)
{
if (x == A[i])
{
cout << "\n The sentence " << x << " Found"; // <--- Changed "founded".
}
else
{
cout << "\n The sentence \"" << x << "\" Not Found";
}
}
// A fair C++ replacement for "system("pause")". Or a way to pause the program.
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();
return 0; // <--- Not required, but makes a good break point.
}
|
It makes the problems with the code easier to spot.
First you should avoid using global variables unless they start with "const" or "constexpr". Later on when your programs get bigger and you are using functions. Any line of code that follows the global variable definition can change the value. This could take hours or even days to track down and fix. The second part of this is that global variables are not needed.
Next I noticed that you wrote:
string x; // <--- Should have a better name.
. By its-self this may not be a problem, but in the "cin" and "cout" statements it is a problem because they do not know how to handle a "std::string" with out the "<string>" header file, which is missing.
In modern C++ it is written as:
int main()
. The "int" is required.
Just like "x" "A" should have a better name. Also the capital "A" gives the impression that it is defined as a constant when it is not. The 2D "char" array is not needed. A 1D array of "std::string"s will work better. It also allows the use of the functions defined in the string class.
I am surprised that line 16 works since you are comparing a "std::string" to a "char" array, but it does seem to work. Comparing 2 "std::strings" would be better.
Your for loop works, but produces this output:
Enter your sentence : Felix buys 1 basketball
The sentence "Felix buys 1 basketball" Not Found
The sentence Felix buys 1 basketball Found
The sentence "Felix buys 1 basketball" Not Found
Press Enter to continue:
|
You also have a problem with the for loop as it is set up to run 1 time. That is 1 of the reasons that you are not getting the correct output.
seeplus's idea of the for loop is something to consider.
Now that you have a better idea of what is wrong it should help to fix what is needed.
Andy