Urgent!!Why is the first string ignored, while the others aren't ??Please thks

Reply

Join Date: Jun 2008
Posts: 11
Reputation: comply or die is an unknown quantity at this point 
Solved Threads: 0
comply or die comply or die is offline Offline
Newbie Poster

Urgent!!Why is the first string ignored, while the others aren't ??Please thks

 
0
  #1
Jun 14th, 2008
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.

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5.  
  6.  
  7. void main(){
  8. void main(){
  9. int NS;
  10. string sentence;
  11. char charsentence[80];
  12. cout<<"How many sentences ?"<<endl;
  13. cin>>NS;
  14. for(int i=0;i<NS;i++){
  15. cout<<"Please enter sentence"<<i+1<<endl;
  16. memset(charsentence,'\0',80);
  17. getline(cin,sentence);
  18. sentence.copy(charsentence,80);
  19. }
  20.  
  21. }
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31. }
Last edited by Ancient Dragon; Jun 14th, 2008 at 11:09 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,757
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 491
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Urgent!!Why is the first string ignored, while the others aren't ??Please thks

 
0
  #2
Jun 14th, 2008
Originally Posted by comply or die View Post
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:

  1. cin >> NS;

  1. 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:

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int N=0;
  8. string *arraystrings2;
  9. cout<<"How many sentences ?"<<endl;
  10. cin >>N;
  11. cin.ignore( numeric_limits<streamsize>::max(), '\n' );
  12. arraystrings2=new string[N];
  13.  
  14. for(int i=0;i<N;i++)
  15. {
  16. cout<<"type sentence " << i+1 << ": ";
  17. getline(cin,arraystrings2[i]);
  18. }
  19.  
  20. for(int i=0;i<N;i++)
  21. {
  22. cout <<arraystrings2[i]<<endl;
  23. }
  24.  
  25. return 0;
  26. }
Last edited by VernonDozier; Jun 14th, 2008 at 10:01 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 11
Reputation: comply or die is an unknown quantity at this point 
Solved Threads: 0
comply or die comply or die is offline Offline
Newbie Poster

Re: Urgent!!Why is the first string ignored, while the others aren't ??Please thks

 
0
  #3
Jun 15th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,757
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 491
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Urgent!!Why is the first string ignored, while the others aren't ??Please thks

 
0
  #4
Jun 15th, 2008
Originally Posted by comply or die View Post
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,383
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 112
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Urgent!!Why is the first string ignored, while the others aren't ??Please thks

 
0
  #5
Jun 15th, 2008
I suggest you get yourself a good book on C++ because it looks like you have no idea what your doing in some places
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-Guid.../dp/0072232153


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

Hope this helps.
I need pageviews! most fun profile ever :)
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 11
Reputation: comply or die is an unknown quantity at this point 
Solved Threads: 0
comply or die comply or die is offline Offline
Newbie Poster

Re: Urgent!!Why is the first string ignored, while the others aren't ??Please thks

 
0
  #6
Jun 15th, 2008
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC