Program Help

I am asked to make the following program:
Write a program in C++:
{a}That asks user to enter a paragraph.
{b}Then search for a sentence (or phrase) in that paragraph if that exist in it or not. The sentence or phrase must be user defined.
I don't know what mistake I have made. can someone correct me with my code. And for the second part of the program I dont know what should I do
My Code is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
#include<stdio.h>

using namespace std;

int main(){
int MAX=500;
    char arr[MAX];

    cout<<"Enter any paragraph which can include spaces or new line.\n";
    cout<<"To exit press the tab key.\n";
    cin>>arr[MAX];

    cout<<"You had entered: \n";
    cout<<arr[MAX];
system ("pause");
    return 0;
}
Last edited on
Your line 8 declares that name 'arr' means an array of 500 char elements.

Your lines 12 and 15 dereference a single char element at position arr+MAX, which is actually outside of the memory block that 'arr' refers to.

Do not use a char array. Use std::string. That will make the task much easier.
What about the part (b) any hints?

I have made the part (a). Any hints for the part (b)

Part (a):
1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>
#include<string>
using namespace std;
int main(){
string abc;
    cout<<"Enter any paragraph which can include spaces or new line.\n";
    getline(cin,abc);
    cout<<"You had entered: \n";
    cout<<abc;
system ("pause");
    return 0;
}
Last edited on
Topic archived. No new replies allowed.