943,643 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 654
  • C++ RSS
Jun 14th, 2008
0

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

Expand 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.

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
comply or die is offline Offline
11 posts
since Jun 2008
Jun 14th, 2008
0

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

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:

C++ Syntax (Toggle Plain Text)
  1. cin >> NS;

C++ Syntax (Toggle Plain Text)
  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:

C++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Jun 15th, 2008
0

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

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
comply or die is offline Offline
11 posts
since Jun 2008
Jun 15th, 2008
0

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

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
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Jun 15th, 2008
0

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

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.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Jun 15th, 2008
0

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

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
comply or die is offline Offline
11 posts
since Jun 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: How do I link Emu8086 to Visual C++ 2008 (Express or Professional Edition)
Next Thread in C++ Forum Timeline: builds in debug but doesn't in realease





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC