English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I currently am having a problem regarding strings and character arrays. What I want to do is have a user type a string like "Johnson 5000" (using cin, not from an input file or preset string), and I want to store this input into two parallel arrays. One for characters while the other is for values. Assume that all variables are properly declared.

for (int canidateNum = 0; canidateNum < ROWS; canidateNum++)
{
cout << "Canidate #" << canidateNum + 1 << ": ";
cin >> name[canidateNum] >> voteNum[canidateNum];
}
cout << name[0] << " " << voteNum[0] << endl;
...

However, upon entering something like "Johnson 5000" in the program, it will output the wrong result, displaying J and not 5000. I tried using various methods of inputing a string into a character array, but to no avail. I tried multi-dimensional arrays, but that didn't work for me either (probably did it wrong).

Can anyone assist me in this?

2007-11-18 19:05:47 · 5 answers · asked by Anonymous in Computers & Internet Programming & Design

5 answers

Your array should be a string array.
A character array will only hold 1 character at each location.
Given that you are just storing the input in one location it is not surprising that it does not work.
If you still want a character array then it must have 2 dimensions. One for each candidate and one for each character of the input.
You then need to read your input one character at a time.

2007-11-18 19:15:03 · answer #1 · answered by AnalProgrammer 7 · 0 1

cout << a[0]; would mean output the single character at the zero position.
cout << a; should output the whole string.

char name[50];
cin >> name;
cout << "Hello " << name;

A string is an array of characters so if you want an array of strings your going to need an array of character arrays. array of array. Now I'm confused. Thank you.

2007-11-18 19:18:26 · answer #2 · answered by Anonymous · 0 1

How you declare your Arrays? correct way to declare an Array is

char name[20]; //string array to hold a name of 21 characters

or
char name[10][20]; /*2D string array to hold 11 names of 21 characters each/*

//to store names use use for loop
for(i=0;i<=10;i++)
cin>>name[i][];

2007-11-18 21:29:28 · answer #3 · answered by Shariq (http://coinsindia.info) 5 · 0 0

i could create 2 int indexes; one for the place you're at on your unique string and yet another for what number characters you have examine forward. Then, count selection by way of your string characters till you notice a ':'. you placed the 1st index to the initiating of the string and increment the 2nd index each and every time you examine a character. in case you notice a ':', then create a substring out of your unique string out of your beginning index as much as the size of characters you examine till you observed the ':'. Then reset the beginning index to the character after the ':' and initiate examining characters back. placed each and every string you come across in a string array. you could the two create a very vast string array which you recognize you will not overfill, or examine in the process the string as quickly as to count selection the style of strings you opt for on your array, then examine by way of it back to examine the strings off and placed them on your array. you will might desire to characteristic code to envision for boundary situations like once you have hit the tip of the string.

2016-09-29 12:18:49 · answer #4 · answered by ? 4 · 0 0

Use a structure
typedef struct
{
char name[80];
long votes;
} candidate_type;

candidate_type candidate[ROWS];
for (int canidateNum = 0; canidateNum < ROWS; canidateNum++)
{
cout << "Canidate #" << canidateNum + 1 << ": ";
cin >> candidate[canidateNum].name;
cin >> candidate[canidateNum].votes;
}
cout << candidate[0].name << " " << candidate0].votes << endl;

2007-11-18 23:09:42 · answer #5 · answered by Anonymous · 0 1

fedest.com, questions and answers