I think I'm getting paranoid

Hello, I have come up stumped with an array issue

I have a string of numbers, "1 32 26 12 8" (with/without whitespace,its just there to display that some are double digits)

If I move them to a char array using a for loop, will the double digit numbers be put in separate cells?

E.g.
Array[0] = 1
Array[1] = 3
Array[2] = 2
Array[3] = 2
Array[4] = 6
etc...


Or will they be left together?
I realise this may well be a stupid question but I'm 30+ without sleep at this point I'm doubting my own name and don't want to continue in the wrong direction

1
2
3
4
5
6
7
string number = "123456"
char Array[];

for(int i =0; i < Array.size -1; i++)
{
   Array[i] = (char)number[i]
}
Last edited on
If I move them to a char array using a for loop, will the double digit numbers be put in separate cells?

Each character will be put in a seperate "cell". That includes whitespace. Like in your example.

Or will they be left together?

Why would c++ think they are somehow together? It can't know. Only you know that.


There are lots of error in your code. Some have semicolons and some dont.
Last edited on
If your string looks like this: "1 32 26 12 8"
then the array assignment would go like this:
Array[0] = '1'
Array[1] = ' '
Array[2] = '3'
Array[3] = '2'
Array[4] = ' '
etc...

By the way, this char Array[]; is an empty array, which is not valid. It is probably a lot simpler/safer to just use std::string which can be treated as an array of characters, and takes care of its own size (if used appropriately).

I only wrote that code as a quick example of my thought pattern, I was sure it was each character to a cell as you've both confirmed, but I thought it best to check :) Thank you

The reason I'm using array is I want to search through the numbers returned by my Alphabet to Decimal code, but I am having trouble loading it into the array as each iteration passes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
int returnVal(char); 
char passwordArray[40];

int main()
{
	string UserPassword;
	cout << "Enter a Password: ";
	cin >> UserPassword;
	string s = UserPassword;

	int numPass;

	
	for (int i = 0; i < UserPassword.length(); i++)
	{
		numPass = returnVal(s[i]);
		passwordArray[i] = numPass;
	}

	cout << numPass;
	cout << passwordArray;
	return 0;
}

int returnVal(char x)
{
	return (int)x;
}
passwordArray is an array of chars. numPass is an integer. You shouldnt do this - passwordArray[i] = numPass;

I'm having trouble understanding what you're trying to do.
I am having trouble loading it into the array as each iteration passes

The code currently works in a fashion, so long as no overflow occurs, the value stored in passwordArray[i] is the same as the original value s[i];

You can see that if for example the code is changed like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <string>
#include <iostream>
using namespace std;


int returnVal(char); 
char passwordArray[40] = {};

int main()
{
    string UserPassword = "HAL";

    string s = UserPassword;

    int numPass;
    
    for (unsigned int i = 0; i < UserPassword.length(); i++)
    {
        numPass = returnVal(s[i]);
        passwordArray[i] = numPass;
    }

    cout << numPass << '\n';
    cout << passwordArray << '\n';
    return 0;
}

int returnVal(char x)
{
    return (int)x + 1;
}

Output:
77
IBM


As an aside, some of the built-in functions such as toupper() are intended to work with character data, but return an int.
http://www.cplusplus.com/reference/cctype/toupper/




@Chervil. Is it fine moving the integer numPass into the char array passwordArray, they're not of the same type? And if so, it's probably not optimal is it?
I don't see why the output is now

1
2
77
IBM


My aim is to have the program run so that whatever the user types in for a password is converted to decimal numbers and loaded into the array of chars

E.g.
User input is "Bone"

Array is:

Array[2]
Array[1]
Array[5]
Array[1]
Array[4]
Array[5]

Because

B = 2
O = 15
N = 14
E = 5
Last edited on
But, B is not 2, O is not 15, N is not 14 and E is not 5. And returnVal returns an int, not a string with digits in it.

http://ideone.com/ElGakU
Last edited on
Is it fine moving the integer numPass into the char array passwordArray, they're not of the same type? And if so, it's probably not optimal is it?

Well, if an int occupies 4 bytes and a char 1 byte, clearly a lot of data is discarded. But so long as the value is in the range +127 to -128 nothing should be lost. Not optimal, I agree.
I don't see why the output is now
77
IBM

Well, 77 is simply the ASCII code for the last letter processed, the letter 'M'.

The other letters are simply the characters of the original string "HAL" increased by 1. The next letter in the alphabet after 'H' is 'I' and so on.

Thanks for describing in more detail what it is you are trying to do, that helps.

It is when I am shifting them to that value! that is the entire point of this okay I can't use capitals, but I am working on using isupper functions to convert any caps a user inputs to lowercaser, I then run it through the returnVal function to convert it to a decimal and subtract the relevant amount from the number eg. a is 97 subtract 96 and I have 1.

But I'm not even fused with the shift value, I just want a consistent decimal result from whatever a user types in, so that I can manipulate what they type into a key for my encryptions
User input is "Bone"

First convert to upper case (use function toupper() on each character).
Next find the position of the character in the alphabet. A naive approach is to simply subtract the letter 'A' and add 1, though there are more robust solutions.

The most tricky part is to convert the resulting number into character format. Since there may be double digits, a simple assignment to an array won't work.

Your specification is ambiguous, I'll suggest something, but it may not be what you want.
1
2
std::ostringstream buffer;
buffer << returnVal(s[i]);


after processing all characters,
std::string passwordArray = buffer.str();
Last edited on
I don't quite understand how to implement what you're saying

it won't let me use std::ostringstream buffer; is just redlines buffer and says it is incomplete?

#include <sstream>
Thank you! that works, well I won't say perfectly as you've all made clear this isn't really the best way to be doing this, but, it works, so thank you very much
Topic archived. No new replies allowed.