| | |
Urgent!!Why is the first string ignored, while the others aren't ??Please thks
![]() |
•
•
Join Date: Jun 2008
Posts: 11
Reputation:
Solved Threads: 0
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.
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)
#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); } } }
Last edited by Ancient Dragon; Jun 14th, 2008 at 11:09 pm. Reason: add code tags
•
•
Join Date: Jan 2008
Posts: 3,757
Reputation:
Solved Threads: 491
Re: Urgent!!Why is the first string ignored, while the others aren't ??Please thks
0
#2 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.
#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)
cin >> NS;
C++ Syntax (Toggle Plain Text)
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)
#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; }
Last edited by VernonDozier; Jun 14th, 2008 at 10:01 pm.
•
•
Join Date: Jun 2008
Posts: 11
Reputation:
Solved Threads: 0
Re: Urgent!!Why is the first string ignored, while the others aren't ??Please thks
0
#3 Jun 15th, 2008
•
•
Join Date: Jan 2008
Posts: 3,757
Reputation:
Solved Threads: 491
Re: Urgent!!Why is the first string ignored, while the others aren't ??Please thks
0
#4 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.
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
•
•
Join Date: Mar 2008
Posts: 1,383
Reputation:
Solved Threads: 112
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.
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 :)
•
•
Join Date: Jun 2008
Posts: 11
Reputation:
Solved Threads: 0
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.
![]() |
Other Threads in the C++ Forum
- Previous Thread: How do I link Emu8086 to Visual C++ 2008 (Express or Professional Edition)
- Next Thread: builds in debug but doesn't in realease
| Thread Tools | Search this Thread |
api array based binary bitmap business c++ c/c++ char class classes code codesamplerunwhilecommands coding commentinghelp compile console conversion count decide delete deploy desktop developer directshow dissertation dll download dynamic dynamiccharacterarray email encryption error faq file forms fstream function functions game givemetehcodez graph guess gui hash homeworkhelp homeworkhelper iamthwee ifpug ifstream incrementoperators infinite input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node output parameter pointer problem proficiency program programming project python random read recursion reference rpg string strings temperature template test text text-file tree url variable vector video win32 windows winsock wordfrequency wxwidgets






