Hi I'm pretty new to programming started about 3 months ago, and even though I feel I'm getting better, I still find it not easy at all, I got especially trouble with that code, look I want to put into an array of strings sentences with spaces, etc.
At last I found out (ppl on this forum told me how) a way to write whole sentences and keep them complete and not just the first word.But now the problem is that I can't type the first line !!!The second, third,fourth,...X line is okay, but not the first it consistently "jumps" over it WHY?,WHY? and when I use rough integers like 4,5 the problem disappears, and as soon as I replace it with an int variable the problem comes again, I'm desperate please Help exam on monday, very important for me, please try the code and see for yourself and thanks of course the forum has already helped me a lot in these two days.

#include <iostream>
#include <string>
using namespace std;



void main(){
                   void main(){
                              int NS; 
                  string sentence;
                  char charsentence[80];
                      cout<<"How many sentences ?"<<endl;
                       cin>>NS;				 		    
   for(int i=0;i<NS;i++){
		   cout<<"Please enter sentence"<<i+1<<endl;
		   memset(charsentence,'\0',80);
		   getline(cin,sentence);
		   sentence.copy(charsentence,80);
                                  }

             }																	 









}

Recommended Answers

All 5 Replies

Hi I'm pretty new to programming started about 3 months ago, and even though I feel I'm getting better, I still find it not easy at all, I got especially trouble with that code, look I want to put into an array of strings sentences with spaces, etc.
At last I found out (ppl on this forum told me how) a way to write whole sentences and keep them complete and not just the first word.But now the problem is that I can't type the first line !!!The second, third,fourth,...X line is okay, but not the first it consistently "jumps" over it WHY?,WHY? and when I use rough integers like 4,5 the problem disappears, and as soon as I replace it with an int variable the problem comes again, I'm desperate please Help exam on monday, very important for me, please try the code and see for yourself and thanks of course the forum has already helped me a lot in these two days.


#include <iostream>
#include <string>
using namespace std;

void main(){
void main(){
int NS;
string sentence;
char charsentence[80];
cout<<"How many sentences ?"<<endl;
cin>>NS;
for(int i=0;i<NS;i++){
cout<<"Please enter sentence"<<i+1<<endl;
memset(charsentence,'\0',80);
getline(cin,sentence);
sentence.copy(charsentence,80);
}

}

}

Did you compile this program? It has "void main" twice. You want "int main" and you only want it once. See my earlier post on your other thread. Is this part of the same problem? You need to flush the input stream after using the >> operator. Stick this line after the line:

cin >> NS;
cin.ignore( numeric_limits<streamsize>::max(), '\n' );

See this link, particularly post 3:
http://www.daniweb.com/forums/thread110008.html

And this link:
http://www.daniweb.com/forums/thread90228.html

And again, check out my post in your last thread. If I understand what you're doing, I don't see the need for memset and copy.

http://www.daniweb.com/forums/thread129286.html

I don't recall having to clear the input stream in that thread, but I did in the code you posted.


[EDIT]
Just looked at my post in the prior thread and ran it. It does need the above statement and it had a typo in it too. I could have sworn it worked when I posted it, but guess not. Oh well, here's the corrected code:

#include <iostream>
#include <string>
using namespace std;

int main()
{
    int N=0;
    string *arraystrings2;
    cout<<"How many sentences ?"<<endl;
    cin >>N;
    cin.ignore( numeric_limits<streamsize>::max(), '\n' );
    arraystrings2=new string[N];

    for(int i=0;i<N;i++)
    {
        cout<<"type sentence " << i+1 << ": ";
        getline(cin,arraystrings2[i]);
    }
							 
    for(int i=0;i<N;i++)
    {
       cout <<arraystrings2[i]<<endl;
    }
    
    return 0;
}

Actually the double void main(), was a mistake on my part, I don't want to convert anything to int, just put full sentences into an array and each sentence into an array of characters, I'll try this code right now Vernon, thank you very much.

Actually the double void main(), was a mistake on my part, I don't want to convert anything to int, just put full sentences into an array and each sentence into an array of characters, I'll try this code right now Vernon, thank you very much.

Okay. If your compiler allows "void main", I guess you can get away with it. In case you're interested, though, here's a few links on why "int main" is the way to go.

http://www.daniweb.com/forums/thread115035.html
http://www.daniweb.com/forums/thread78955.html
http://www.daniweb.com/forums/thread83820.html

I suggest you get yourself a good book on C++ because it looks like you have no idea what your doing in some places :icon_neutral:
You should also learn to format your code better, you have about 10 empty lines near the end and your indentation is messed up.

I have recommended this book a few times because it is very easy to follow and teaches you good techniques on programming.

C++ A Beginner's Guide Second Edition by Herbert Schildt.
http://www.amazon.com/Beginners-Guide-Second-Guides-McGraw-Hill/dp/0072232153


If you decide not to get that, you can also read a decent tutorial, like this one.
http://www.cplusplus.com/doc/tutorial/program_structure.html

Hope this helps.

Yes Vernon you're right I'm not quite there yet (very far), the thing is that I've started C++ about 8 weeks ago, and it wasn't very intensive like 24/7 programing, I believe it's excusable, and thanks for the recommendations, I'll certainly buy these books very very soon, because I want to be good at CPP, not master it (I doubt I'd be able to), but still be decent at it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.