little help with arrays?

Walleye

Limp Gawd
Joined
Jan 13, 2004
Messages
432
gawd, this project would be so much easier if i could just use a switch...

anyway, can someone tell me why this:

char score[] = {2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 9};

cout << score[20] << "outputting a score to verify the array is working" << endl;

outputs a period?

quite literally, it puts out a "." :(

i've traced my problem to this..i've tried every damn way i can think of to init the array, every time results in this.
 
Maybe because you're outputting ordinals instead of character literals? chars below 32 aren't intended for display.
 
lomn75 said:
Maybe because you're outputting ordinals instead of character literals? chars below 32 aren't intended for display.

being that i've never heard of this before, and it wasnt covered in any lesson, how would i get around this, accepting that i have to have values based on those appended to a string at some point?
 
Are you trying to output the value at possition 20 in the array (ie "8")? If so
Code:
printf("%d", score[20]);
would do it.
 
You could either:
1 - declare the array as an integer array
2 - cast the result to an integer (ex: cout << (int)score[20] << endl;)
 
alright, i got that one item working (odd..it suddenly worked 0.o)

but the string i output at the end still doesnt work.

here's how my code looks for the whole project. apparently it wasnt my problem :(

Code:
#include <iostream>
using namespace std;
// Program takes a user inputted 7 character string, and
// puts it through a filter resulting in a 7 digit telephone
// number.
// characters passed to the function result in returning what
// number in the alphabet they are, and asking the array what
// number corresponds with that entry yields a phone # digit
string phone(string name);
int dig(char alph);

int main()
{
	string name;
	cout << "Mnemonic must be 7 characters in length, and lowercase";
	cout << "\nPlease enter the mnemonic to be decoded: ";
	cin >> name;
	if(name.size() != 7)
	{
		cout << "The mnemonic must be 7 characters in length, and in lowercase!";
		return main();
	}
	string phonenum;
	phonenum = phone(name);
	cout << "The Phonenumber determined by the Mnemonic is: ";
	cout << phonenum;
	cout << endl;	
	return 0;
}

string phone(string name)
{
	cout << "stub for phone(string name)" << endl;
	string pn;
	int score[] = {2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 9};
	cout << score[20] << "  outputting a score to verify the array is working" << endl;
	for(int i = 0; i < 7; i++)
	{
		cout << "stub for for loop" << endl;
		int h = dig(name[i]);
		pn.push_back(score[h]);
	}
	return pn;
}

int dig(char alph)
{
	cout << "Stub for dig(alpha)" << endl;
	string alphabet = "abcdefghijklmnopqrstuvwxyz";
	int pos = alphabet.find(alph);
	return pos;
}
 
Change your array to:
Code:
char score[] = {'2', '2', '2', '3', '3', '3', '4', '4', '4', '5', '5', '5', '6', '6', '6', '7', '7', '7', '7', '8', '8', '8', '9', '9', '9', '9'};
 
SledgY said:
Change your array to:
Code:
char score[] = {'2', '2', '2', '3', '3', '3', '4', '4', '4', '5', '5', '5', '6', '6', '6', '7', '7', '7', '7', '8', '8', '8', '9', '9', '9', '9'};


i <3 you.

why the hell didnt i think of that.... *sigh*

thanks very much! you just saved 1/5 of my project.
 
alright..i've been googling this one and i cant seem to find the proper syntax for getline to read into a string. everything i see is char arrays.. not a string. and unless i'm mistaken, i cant pass arrays to and from functions. 0.o
 
Lord of Shadows said:
lol, I wouldnt consider putting quotes around characters something that requires much experience
ya, but remembering where you failed before does ;)
 
Lord of Shadows said:
lol, I wouldnt consider putting quotes around characters something that requires much experience
Spotting errors in code can take a bit. Even the most basic.
 
Walleye said:
alright..i've been googling this one and i cant seem to find the proper syntax for getline to read into a string. everything i see is char arrays.. not a string. and unless i'm mistaken, i cant pass arrays to and from functions. 0.o
In C a "string" is a char array.
 
SledgY said:
In C a "string" is a char array.

i'm working in c++, where we get a wonderful string class that makes things 10,000 times easier...except in this project.

it's ok tho, i now have it to where it reads into the char array, then makes that into a string.

it compiled okay, but now for some reason it kills and terminates the program when i excecute it. probably cause the line doesnt include 80 characters, but the loop wouldnt read the size of the filled portions of the array, so i had to just make it read up to 80, and now it tries to append empty spots of an array to a string.

i dont wanna bother figuring it out lol. i got 3 more programming projects to do.
 
Walleye said:
alright..i've been googling this one and i cant seem to find the proper syntax for getline to read into a string. everything i see is char arrays.. not a string. and unless i'm mistaken, i cant pass arrays to and from functions. 0.o

getline(istream, string, delim);

delimeter defaults to a newline, example....

getline(cin, myString);

If you can be more specific with your error we can try to help =)
 
i'm working in c++, where we get a wonderful string class that makes things 10,000 times easier...except in this project.
Just because there is a string class doesn't mean you have to use it. C++ is just an extension of C so anything from C will generally work in C++. In some cases using a string class is more of a [font=&quot]hindrance [/font]than actually useful.
 
yeah you can. Pass a pointer to the array. example is
int blah(char stuff[] )

Walleye said:
alright..i've been googling this one and i cant seem to find the proper syntax for getline to read into a string. everything i see is char arrays.. not a string. and unless i'm mistaken, i cant pass arrays to and from functions. 0.o
 
Back
Top