nanaster Posted November 4, 2005 Share Posted November 4, 2005 Hello there.im a young newb who is learning the c++ programming this year.i was given a specific task to do but then its really really hard for me.i have never learnt bout this before and its giving me a pain in the a**. i also have "c++ how to program", and spent like 2 days reading it but it seems like i can't use the principles from the book to solve teh tasks...it would be so generous of you if u could help me out on this task and simply explain why its like that after if u wish..the task is that i hv to write a c++ prog (source) that asks for an arbitrary no. of input strings n a search pattern. then it outputs the no. of strings that match the search pattern and the matched strings.this is an eg of the behaviour of the prog.csh> ./ex3Input string (enter X to stop): Imagine there's no heavenInput string (enter X to stop): It's easy if you tryInput string (enter X to stop): No hell below usInput string (enter X to stop): Imagine there's no countriesInput string (enter X to stop): You may say I'm a dreamerInput string (enter X to stop): But I'm not the only oneInput string (enter X to stop): XSearch pattern: noNumber of matched strings: 3Imagine there's no heavenImagine there's no countriesBut I'm not the only onethats the behavour the prog should be like... the user enters any no. of strings until there is a single X input. the prog would search each input string and see if any of htem contains the search pattern. the search is by default case sensitive. teh two things that should be out-putted is the no of matched strings and all the matched strings in teh same order of the input...I would appreciate it so much if you could help me learn make this prog....i hv been spending like few entire days on this but i really relaly can't get it... thank you so much,.... Link to comment Share on other sites More sharing options...
liquidplasmaflow Posted November 5, 2005 Share Posted November 5, 2005 Well, start off by telling us how much C++ you know so far; run us through the basics. And if I weren't so busy right now, I'd code it for you. Link to comment Share on other sites More sharing options...
nanaster Posted November 5, 2005 Author Share Posted November 5, 2005 this is wat i got so far....please chek ur hotmail..#include <iostream> #include <cstring> int main() { int x = 0,y=0,total; char ans[100]; char *al[100]; char pattern[100]; char *match[100]; while (true) { cout << "Input string (enter X to stop): "; cin.getline(ans,100); if (ans[0] == 'X' && ans[1] == '\0') break; al[x] = new char[(strlen(ans)+1)]; strcpy( al[x], ans ); x++; } total = x; cout << "Search Pattern: "; cin >> pattern; for (x=0;x<total;x++) { if (strstr(al[x],pattern)) { match[y] = al[x]; y++; } } total = y; cout << "Number of matches: " << total << endl; for(x=0;x<total;x++) { cout << match[x] << endl; } return 0; } Link to comment Share on other sites More sharing options...
phkninja Posted November 27, 2005 Share Posted November 27, 2005 Your code worked for me, just had to addusing std::cin;using std::cout;using std::endl;and a carrige return to the end of the file to make the compiler remove all warnings and error messages Link to comment Share on other sites More sharing options...
zivan56 Posted December 1, 2005 Share Posted December 1, 2005 Your code worked for me, just had to addusing std::cin;using std::cout;using std::endl;and a carrige return to the end of the file to make the compiler remove all warnings and error messagesYou can spare yourself the hassle by using:using namespace std;After the includes Link to comment Share on other sites More sharing options...
phkninja Posted December 1, 2005 Share Posted December 1, 2005 i kinda know that, i was doing it quickly and never thought about what i wrote (brain was in auto). Anyway give me a break im an elctronics engineer and not a programmer ( i teach myself programming in my spare thime. thats why i reach a certain level and usually stop, can get the info to go further.)i kinda know that, i was doing it quickly and never thought about what i wrote (brain was in auto). Anyway give me a break im an elctronics engineer and not a programmer ( i teach myself programming in my spare thime. thats why i reach a certain level and usually stop, can get the info to go further.) Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now