String of integers
-
Im trying to create a simple program the reads a string of integers from the board and store them in an array, then out put to the screen each integer from that string. It compiles fine but then crashes with an error. This is my code:
// PassArray.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include conio.h #include iostream using namespace std; int _tmain(int argc, _TCHAR* argv[]) { int aArray[4]; int n; cout << "Input 5 digits: "; cin >> aArray[4]; for ( n=0; n<0; n++ ) { cout << "Integer: " << aArray[n] << endl; } _getch(); return 0; }
this is the error Run-Time Check Failure #2 - Stack around the variable 'aArray' was corrupted. -
The last element of aArray if aArray[3], but not aArray[4]. And what do you mean by:
Herboren wrote:
for ( n=0; n<0; n++ )
forgive me I had noticed that earlier I corrected it. Edit:
#include "stdafx.h" #include <> #include <> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { int aArray[4]; int n; cout << "Input 5 digits: "; cin >> aArray[0]; for ( n=0; n<5; n++ ) { cout << "Integer: " << aArray[n] << endl; } _getch(); return 0; }
of course knowing the array is 5 integers long '01234'. Basically the program is supposed to read a string of integers from the user, for example, input '12345' all together on the first line. Then each value within that string is stored in an array therefore if I wanted to print the first value of that array the value would come out as the '1' and so on. Now when you build the code it asks for an input the downside to this is it asks for 5 elements as a string or individual, in 5 different columns. so if you typed '12345' you would have to press enter and repeat that 4 more times, which is what im trying to avoid. The operation of the program is supposed to ask for the string of integers once and place each element of the string into an array therefor the string '12345' would be placed in aArray[4] thus giving me 5 elements in in one column. smell my drift? ;P -
forgive me I had noticed that earlier I corrected it. Edit:
#include "stdafx.h" #include <> #include <> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { int aArray[4]; int n; cout << "Input 5 digits: "; cin >> aArray[0]; for ( n=0; n<5; n++ ) { cout << "Integer: " << aArray[n] << endl; } _getch(); return 0; }
of course knowing the array is 5 integers long '01234'. Basically the program is supposed to read a string of integers from the user, for example, input '12345' all together on the first line. Then each value within that string is stored in an array therefore if I wanted to print the first value of that array the value would come out as the '1' and so on. Now when you build the code it asks for an input the downside to this is it asks for 5 elements as a string or individual, in 5 different columns. so if you typed '12345' you would have to press enter and repeat that 4 more times, which is what im trying to avoid. The operation of the program is supposed to ask for the string of integers once and place each element of the string into an array therefor the string '12345' would be placed in aArray[4] thus giving me 5 elements in in one column. smell my drift? ;P -
Im trying to create a simple program the reads a string of integers from the board and store them in an array, then out put to the screen each integer from that string. It compiles fine but then crashes with an error. This is my code:
// PassArray.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include conio.h #include iostream using namespace std; int _tmain(int argc, _TCHAR* argv[]) { int aArray[4]; int n; cout << "Input 5 digits: "; cin >> aArray[4]; for ( n=0; n<0; n++ ) { cout << "Integer: " << aArray[n] << endl; } _getch(); return 0; }
this is the error Run-Time Check Failure #2 - Stack around the variable 'aArray' was corrupted. -
Im trying to create a simple program the reads a string of integers from the board and store them in an array, then out put to the screen each integer from that string. It compiles fine but then crashes with an error. This is my code:
// PassArray.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include conio.h #include iostream using namespace std; int _tmain(int argc, _TCHAR* argv[]) { int aArray[4]; int n; cout << "Input 5 digits: "; cin >> aArray[4]; for ( n=0; n<0; n++ ) { cout << "Integer: " << aArray[n] << endl; } _getch(); return 0; }
this is the error Run-Time Check Failure #2 - Stack around the variable 'aArray' was corrupted.Herboren wrote:
Im trying to create a simple program the reads a string of integers ... int aArray[4];
These two seem to contradict each other. You either meant to say, "Im trying to create a simple program the reads an array of integers," or you need to declare:
Herboren wrote:
for ( n=0; n<0; n++ )
This loop will not do what you expect, unless you expect it to execute 0 times.
string aArray[4];
In any case, consider:
int aArray[4];
cin >> aArray[0];
cin >> aArray[1];
cin >> aArray[2];
cin >> aArray[3];or
for (int n = 0; n < 3; n++) // the 3 should not be hard-coded
cin >> aArray[n];Notice the benefits of the latter example?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Herboren wrote:
Im trying to create a simple program the reads a string of integers ... int aArray[4];
These two seem to contradict each other. You either meant to say, "Im trying to create a simple program the reads an array of integers," or you need to declare:
Herboren wrote:
for ( n=0; n<0; n++ )
This loop will not do what you expect, unless you expect it to execute 0 times.
string aArray[4];
In any case, consider:
int aArray[4];
cin >> aArray[0];
cin >> aArray[1];
cin >> aArray[2];
cin >> aArray[3];or
for (int n = 0; n < 3; n++) // the 3 should not be hard-coded
cin >> aArray[n];Notice the benefits of the latter example?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
Russel *Sigh*, if you have a little more to tell me than just "NO" i might actually listen to you. Crow even turning it to a string wouldnt that mean its still in char format and not int. I should be able to add an unspecified amount to each element of the string inside the array. Like adding a 1 to 12345 making it 22345.
#include "stdafx.h" #include #include #include #include #include using namespace std; int _tmain(int argc, _TCHAR* argv[]) { int aArray[4]; int u,d,t,q,c,a; string st; cout << "Input 5-digits: "; cin >> st; cout << st << endl; _getch(); istringstream ss(st); ss >> u; aArray[0] = u; if ( ss.peek() != '\n' ) { cerr<<"Invalid format"<> d; aArray[1] = d; if ( ss.peek() != '\n' ) { cerr<<"Invalid format"<> t; aArray[2] = t; if ( ss.peek() != '\n' ) { cerr<<"Invalid format"<> q; aArray[3] = q; if ( ss.peek() != '\n' ) { cerr<<"Invalid format"<> c; aArray[4] = c; cout << u << d << t << q << c << " " << aArray[0] << aArray[1] << aArray[2] << aArray[3] << aArray[4] << endl; cout << aArray[0] << endl; _getch(); return 0; }
-- modified at 10:49 Tuesday 18th September, 2007 -
Russel *Sigh*, if you have a little more to tell me than just "NO" i might actually listen to you. Crow even turning it to a string wouldnt that mean its still in char format and not int. I should be able to add an unspecified amount to each element of the string inside the array. Like adding a 1 to 12345 making it 22345.
#include "stdafx.h" #include #include #include #include #include using namespace std; int _tmain(int argc, _TCHAR* argv[]) { int aArray[4]; int u,d,t,q,c,a; string st; cout << "Input 5-digits: "; cin >> st; cout << st << endl; _getch(); istringstream ss(st); ss >> u; aArray[0] = u; if ( ss.peek() != '\n' ) { cerr<<"Invalid format"<> d; aArray[1] = d; if ( ss.peek() != '\n' ) { cerr<<"Invalid format"<> t; aArray[2] = t; if ( ss.peek() != '\n' ) { cerr<<"Invalid format"<> q; aArray[3] = q; if ( ss.peek() != '\n' ) { cerr<<"Invalid format"<> c; aArray[4] = c; cout << u << d << t << q << c << " " << aArray[0] << aArray[1] << aArray[2] << aArray[3] << aArray[4] << endl; cout << aArray[0] << endl; _getch(); return 0; }
-- modified at 10:49 Tuesday 18th September, 2007Herboren wrote:
*Sigh*, dont worry bout it dude, I must not be making myself clear enough I dont know any other basic way to put it. I got better results somewhere else. sheesh!
That's fine, but remember, you're the one asking for help, not us.
Herboren wrote:
Crow even turning it to a string...
I was not suggesting using one type over another. I was merely pointing out that the type you do use ultimately determines what API/classes you need. For example, if you say you want to add a bunch of numbers together, but provide a code snippet that uses
string
s, there's obviously going to be extra work involved.Herboren wrote:
I should be able to add an unspecified amount to each element of the string inside the array. Like adding a 1 to 12345 making it 22345.
And how does the code snippet you've shown do any of this? What exactly are you wanting the user to input (there's a big difference between five numbers or digts, and a 5-digit number)? This all may sound pedantic, but if we can't understand what it is that you want, how could you possibly expect an answer?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Herboren wrote:
*Sigh*, dont worry bout it dude, I must not be making myself clear enough I dont know any other basic way to put it. I got better results somewhere else. sheesh!
That's fine, but remember, you're the one asking for help, not us.
Herboren wrote:
Crow even turning it to a string...
I was not suggesting using one type over another. I was merely pointing out that the type you do use ultimately determines what API/classes you need. For example, if you say you want to add a bunch of numbers together, but provide a code snippet that uses
string
s, there's obviously going to be extra work involved.Herboren wrote:
I should be able to add an unspecified amount to each element of the string inside the array. Like adding a 1 to 12345 making it 22345.
And how does the code snippet you've shown do any of this? What exactly are you wanting the user to input (there's a big difference between five numbers or digts, and a 5-digit number)? This all may sound pedantic, but if we can't understand what it is that you want, how could you possibly expect an answer?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
Forgive me if I may have sounded crude. I have researched google dreamincode codeguru among other search engines and did not find my answer /cry, sorry im just frustrated. This is role of my program. Think of it as a uname/pword program which leads to a basic encryption or start of it: --role-- Please input a 5 digit number: 78265 Your input was 78265, is this correct? y --role-- Now, knowing user input was a 5 digit number, that should then be broken down into and array of integers: 78265 Each element of the 5 digit number would be placed into an array after user input ie: 7,8,2,6,5
-
Forgive me if I may have sounded crude. I have researched google dreamincode codeguru among other search engines and did not find my answer /cry, sorry im just frustrated. This is role of my program. Think of it as a uname/pword program which leads to a basic encryption or start of it: --role-- Please input a 5 digit number: 78265 Your input was 78265, is this correct? y --role-- Now, knowing user input was a 5 digit number, that should then be broken down into and array of integers: 78265 Each element of the 5 digit number would be placed into an array after user input ie: 7,8,2,6,5
Much clearer. How about something like:
string strNumber = "78265";
int nDigits[5];
nDigits[0] = strNumber[0] - '0';
nDigits[1] = strNumber[1] - '0';
nDigits[2] = strNumber[2] - '0';
nDigits[3] = strNumber[3] - '0';
nDigits[4] = strNumber[4] - '0';
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Much clearer. How about something like:
string strNumber = "78265";
int nDigits[5];
nDigits[0] = strNumber[0] - '0';
nDigits[1] = strNumber[1] - '0';
nDigits[2] = strNumber[2] - '0';
nDigits[3] = strNumber[3] - '0';
nDigits[4] = strNumber[4] - '0';
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
strNumber cannot be declared in the program. The strNumber has to ask for users input, because the number could be anything that user specifies. smell my drift? :laugh:. There has to be a way to store the users input '12345' in an array after they type it in. -- modified at 15:09 Tuesday 18th September, 2007 Here is antoher extended role --role Please type in a five digit number: 79135 //Ask for user input The number you typed was'79135' is this correct? y //Is input correct y/n The first number '7' from 79135 will be stored in aArray[0]. //store 7 in array0 The second number '9' from 79135 will be stored in aArray[1]. //store 9 in array0 The third number '1' from 79135 will be stored in aArray[2]. //store 1 in array0 The fourth number '3' from 79135 will be stored in aArray[3]. //store 3 in array0 The fifth number '5' from 79135 will be stored in aArray[4]. //store 5 in array0 Each element of the 5 digit number from user input have been stored individually in the array[5] --role--
-
strNumber cannot be declared in the program. The strNumber has to ask for users input, because the number could be anything that user specifies. smell my drift? :laugh:. There has to be a way to store the users input '12345' in an array after they type it in. -- modified at 15:09 Tuesday 18th September, 2007 Here is antoher extended role --role Please type in a five digit number: 79135 //Ask for user input The number you typed was'79135' is this correct? y //Is input correct y/n The first number '7' from 79135 will be stored in aArray[0]. //store 7 in array0 The second number '9' from 79135 will be stored in aArray[1]. //store 9 in array0 The third number '1' from 79135 will be stored in aArray[2]. //store 1 in array0 The fourth number '3' from 79135 will be stored in aArray[3]. //store 3 in array0 The fifth number '5' from 79135 will be stored in aArray[4]. //store 5 in array0 Each element of the 5 digit number from user input have been stored individually in the array[5] --role--
Herboren wrote:
strNumber cannot be declared in the program. The strNumber has to ask for users input, because the number could be anything that user specifies. smell my drift?
Yes, and what I showed was merely an example. I'm not in the position of providing all of the
cin
/cout
statements that you require. Even new developers know thatstring strNumber = "79135"
can be turned into a user input-able statement with but one line of code. How a value gets assigned to a variable, either via the keyboard or as a string literal, is irrelevant.Herboren wrote:
The first number '7' from 79135 will be stored in aArray[0]. //store 7 in array0 The second number '9' from 79135 will be stored in aArray[1]. //store 9 in array0 The third number '1' from 79135 will be stored in aArray[2]. //store 1 in array0 The fourth number '3' from 79135 will be stored in aArray[3]. //store 3 in array0 The fifth number '5' from 79135 will be stored in aArray[4]. //store 5 in array0
Why would you want all five digits stored in
array0
? Furthermore, what isarray0
?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Herboren wrote:
strNumber cannot be declared in the program. The strNumber has to ask for users input, because the number could be anything that user specifies. smell my drift?
Yes, and what I showed was merely an example. I'm not in the position of providing all of the
cin
/cout
statements that you require. Even new developers know thatstring strNumber = "79135"
can be turned into a user input-able statement with but one line of code. How a value gets assigned to a variable, either via the keyboard or as a string literal, is irrelevant.Herboren wrote:
The first number '7' from 79135 will be stored in aArray[0]. //store 7 in array0 The second number '9' from 79135 will be stored in aArray[1]. //store 9 in array0 The third number '1' from 79135 will be stored in aArray[2]. //store 1 in array0 The fourth number '3' from 79135 will be stored in aArray[3]. //store 3 in array0 The fifth number '5' from 79135 will be stored in aArray[4]. //store 5 in array0
Why would you want all five digits stored in
array0
? Furthermore, what isarray0
?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
it was supposed to say //store in aArray[0] //example //store in aArray[1] //example //store in aArray[2] //example //store in aArray[3] //example //store in aArray[4] //example this was only an example, this has nothing to do with my code this was just a role just so you could better understand what I was trying to say. Yes, and what I showed was merely an example. I'm not in the position of providing all of the cin/cout statements that you require. I know it was an exmaple and im not asking you to tell what the cin and cout is. I know I can grab sets of digits as a string, I know basically everything you just told me. I dont know any other basic way to put it, i have role played it for you twice now and you still dont seem to understand what im saying. Reverting back to the 2nd question I asked yesterday, The problem I am having is storing each e-l-e-m-e-n-t of the "string" into an a-r-r-a-y.
-
it was supposed to say //store in aArray[0] //example //store in aArray[1] //example //store in aArray[2] //example //store in aArray[3] //example //store in aArray[4] //example this was only an example, this has nothing to do with my code this was just a role just so you could better understand what I was trying to say. Yes, and what I showed was merely an example. I'm not in the position of providing all of the cin/cout statements that you require. I know it was an exmaple and im not asking you to tell what the cin and cout is. I know I can grab sets of digits as a string, I know basically everything you just told me. I dont know any other basic way to put it, i have role played it for you twice now and you still dont seem to understand what im saying. Reverting back to the 2nd question I asked yesterday, The problem I am having is storing each e-l-e-m-e-n-t of the "string" into an a-r-r-a-y.
Herboren wrote:
it was supposed to say //store in aArray[0] //example //store in aArray[1] //example //store in aArray[2] //example //store in aArray[3] //example //store in aArray[4] //example this was only an example,
And is exactly what I showed you in my code snippet.
Herboren wrote:
The problem I am having is storing each e-l-e-m-e-n-t of the "string" into an a-r-r-a-y.
I've showed you how to do this twice, using two different approaches. What are you not understanding?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Herboren wrote:
it was supposed to say //store in aArray[0] //example //store in aArray[1] //example //store in aArray[2] //example //store in aArray[3] //example //store in aArray[4] //example this was only an example,
And is exactly what I showed you in my code snippet.
Herboren wrote:
The problem I am having is storing each e-l-e-m-e-n-t of the "string" into an a-r-r-a-y.
I've showed you how to do this twice, using two different approaches. What are you not understanding?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
Holy shat dude it worked. you are the bomb digitty. Heh I knew I could get user input by doing cin >> string; which normally a string is a chain of characters I guess I should say symbols and I thought the string had to be converted to integers first before storing them into and array. yeah I know char's can be stored in arrays to but I had planned on adding different numerals to each int stored in the array, so i assumed i wouldnt be able to add numberals to char's stored in the array, thats like saying 1 + a or 2 + b which then would result in an error of some sort. But it worked perfectly all thanks to you. sorry if i may have sounded a little edgy, it was out of my perspective which is why i was not understanding. Okay so the part I dont understand is the "- '0'" when converting the str to the array. Awesome im glad we completed that. Now here is another doosy. I need to create an array based on the amount of symbols in the str, so instead of pre.creating the 'aArray[5]' im using the strlen(str.c_str()) to count the number of symbols in the str from the users input. this is my code so far:
// ArrayInput.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include #include using namespace std; int _tmain(int argc, _TCHAR* argv[]) { string str; int aArray[5]; int n; cout << "Input a 5-digit number: "; cin >> str; aArray[0] = str[0] - '0'; aArray[1] = str[1] - '0'; aArray[2] = str[2] - '0'; aArray[3] = str[3] - '0'; aArray[4] = str[4] - '0'; //strlen() cout << "The length of " << str << " is: " << strlen( str.c_str()) << endl; _getch(); return 0; }
-- modified at 17:54 Tuesday 18th September, 2007 -
Holy shat dude it worked. you are the bomb digitty. Heh I knew I could get user input by doing cin >> string; which normally a string is a chain of characters I guess I should say symbols and I thought the string had to be converted to integers first before storing them into and array. yeah I know char's can be stored in arrays to but I had planned on adding different numerals to each int stored in the array, so i assumed i wouldnt be able to add numberals to char's stored in the array, thats like saying 1 + a or 2 + b which then would result in an error of some sort. But it worked perfectly all thanks to you. sorry if i may have sounded a little edgy, it was out of my perspective which is why i was not understanding. Okay so the part I dont understand is the "- '0'" when converting the str to the array. Awesome im glad we completed that. Now here is another doosy. I need to create an array based on the amount of symbols in the str, so instead of pre.creating the 'aArray[5]' im using the strlen(str.c_str()) to count the number of symbols in the str from the users input. this is my code so far:
// ArrayInput.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include #include using namespace std; int _tmain(int argc, _TCHAR* argv[]) { string str; int aArray[5]; int n; cout << "Input a 5-digit number: "; cin >> str; aArray[0] = str[0] - '0'; aArray[1] = str[1] - '0'; aArray[2] = str[2] - '0'; aArray[3] = str[3] - '0'; aArray[4] = str[4] - '0'; //strlen() cout << "The length of " << str << " is: " << strlen( str.c_str()) << endl; _getch(); return 0; }
-- modified at 17:54 Tuesday 18th September, 2007Herboren wrote:
cout << "The length of " << str << " is: " << strlen( str.c_str()) << endl;
Since you are using a
string
object:cout << "The length of " << str << " is: " << str.length() << endl;
Herboren wrote:
I need to create an array based on the amount of symbols in the str, so instead of pre.creating the 'aArray[5]' im using the strlen(str.c_str()) to count the number of symbols in the str from the users input.
int *nArray = new int[what_number_goes_here?];
...
delete [] nArray;
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne