| | |
String Input & Output
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2007
Posts: 49
Reputation:
Solved Threads: 0
c++ Syntax (Toggle Plain Text)
// Stopping input with an empty string #include <iostream> using namespace std; #include <cstring> int main() { int i = 1; char temp[80]; char string[80]; // room for 255 strings cout << "\n\nEnter some strings - (blank to exit)" << " \nfirst string: "; cin.getline(temp, 80); while (temp[0] != '\0') { cout << "String " << i << " : " << temp << "\nnext string: "; cin.getline(temp, 80); string == strcat(string," "); string == strcat(string,temp); i++; } cout << "\n\n"; //for (int index = 0; index < i; index++) cout << string ; }
i updated the code
input: bill loves to eat
output: $ loves to eat
$ is a weird character that show up. i don't know what it is.
Last edited by nicz888; Dec 16th, 2007 at 9:54 pm.
You misunderstand something about pointers.
"temp" is an array of 80 characters. There is now space reserved in memory for 80 characters. The name "temp" by itself is a pointer to the first character in the array.
"array" is an array of 255 pointers to characters. There is now space reserved in memory for 255 pointers (not characters). The name "array" by itself is a pointer to the first pointer in the array. (BTW, don't name things "array".)
So, if you say
If you then say
In your loop, you assign array[ n ] (a pointer to a character) the address of the first char in the temp array. So,
Your loop is failing because "i" starts at one when it should start at zero. In other words, when you increment "i" you are saying that there is one more item in the "array" than there really is.
Hope this helps.
[EDIT] You know, it really is difficult to help when you change things after posting.
I'll check again after you've made up your mind.
"temp" is an array of 80 characters. There is now space reserved in memory for 80 characters. The name "temp" by itself is a pointer to the first character in the array.
"array" is an array of 255 pointers to characters. There is now space reserved in memory for 255 pointers (not characters). The name "array" by itself is a pointer to the first pointer in the array. (BTW, don't name things "array".)
So, if you say
strcpy( temp, "Hello" ); then the temp[] array contains the string "Hello".If you then say
strcpy( temp, "world" ); then the temp[] array contains the string "world". The string "Hello" is overwritten, and no longer exists.In your loop, you assign array[ n ] (a pointer to a character) the address of the first char in the temp array. So,
C++ Syntax (Toggle Plain Text)
strcpy( temp, "Hello" ); // temp[] contains "Hello" array[ 0 ] = temp; // array[ 0 ] points to "Hello" in temp[] strcpy( temp, "world" ); // temp[] contains "world" // array[ 0 ] still points to temp[], but temp[] now // contains "world"
Your loop is failing because "i" starts at one when it should start at zero. In other words, when you increment "i" you are saying that there is one more item in the "array" than there really is.
Hope this helps.
[EDIT] You know, it really is difficult to help when you change things after posting.
I'll check again after you've made up your mind.
char string[80]; only creates an array of 80 characters. The array is named "string". There is a problem with the order in which you are doing things. You get another string from the user before you copy "bill" to the "string" array.
The weird character is because you did not initialize your "string" array properly. You are lucky the program didn't crash.
Somewhere at the top, say line 11 or so, say
string[0] = '\0';Hope this helps.
PS. Don't name arrays "string" either. Come up with a real name.
[EDIT] Oh yeah, strcat_s() is non-standard MS crap. If you are only going to use the MS compiler, then fine, else stick with strcat().
Last edited by Duoas; Dec 16th, 2007 at 10:18 pm.
![]() |
Similar Threads
- error: no matching function for call to 'getline(std::ofstream&, std::string&, char)' (C++)
- Form data & Back button (PHP)
- Pascal : Variable for both integer & string? (Pascal and Delphi)
- Average Grade - Functions w/istreams & fstreams (C++)
- Bubble sort & File output jibrish errors??? (C)
- ASP.net/Stored Proc & Login Verification (ASP.NET)
- line up JTextField GUI java & write to file (Java)
Other Threads in the C++ Forum
- Previous Thread: Rearranging and Sorting C strings.
- Next Thread: #define ID_...?
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets







(Kind of like "Can I ask a question?" heh heh heh...)