Variable grab

I have an array with a numerical value in it, "311818152", I am trying to write a method that will cycle through the array and put the values of the first 2 cells into 2 variable "test1" "test2"

but the output is just '0' for both when I print for some reason?

when the intend output should be

test1 = "3"
test2 = "1"

Where am I going wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  int variableGrab()
{
	for (int i = 0; i < passwordArray[i]; i++)
	{
		if (i == 0)
		{
			test1 = passwordArray[i];
		}
		else if (i == 1)
		{
			test2 = passwordArray[i];
		}
		else
		{
			return 0;
		}
	}
	return 0;
}
Last edited on
That is one weird looking for-loop if I've ever seen one. If you want to cycle through the array then you run the for-loop array_lenght amount of times. i < passwordArray[i]; // what is this ?

It always helps when the program is compilable, so perhaps you could create a small program which reproduces your problem and compiles.

It looks like you specifically want only the first two values in the array, so why not just run the for-loop twice?
Last edited on
Sorry I've been playing with it and just grasping at straws so when I pasted it in, it was whatever I'd left it with

Believe it or not, it compiles as it is there, which is part of the problem, its not giving any errors so I don't know whats wrong, yeah I only want the first 2 values of the array as I intend to delete as soon as they have been copied into the integers
Believe it or not, it compiles as it is there
It most certainly does not. It does for you because you have the rest of the program, but we don't.

If you want the first two values in the array. Do it like this -

1
2
3
4
5
6
7
8
9
10
11
for (int i = 0; i < 2; i++)
	{
		if (i == 0)
		{
			test1 = passwordArray[i];
		}
		else if (i == 1)
		{
			test2 = passwordArray[i];
		}
	}
Last edited on
I made those changes and it still returns 0 for both integers

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>

int returnVal(char);
int noCaps();
string UserPassword;
std::ostringstream buffer;
string passwordArray;
int variableGrab();
int test1;
int test2;

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

	for (unsigned int i = 0; i < UserPassword.length(); i++)
	{
		cout << returnVal(s[i]);
	}
	passwordArray = buffer.str();
	cout << passwordArray << endl;
	int variableGrab();
	cout << test1 << endl;
	cout << test1 << endl;
}

int returnVal(char x)
{
	return (int)x - 96;
}

int noCaps()
{
	for (int i = 0; i < UserPassword[i]; i++) // Step through each of the characters in the array named 'UserPassword'
		if (UserPassword[i] != 'x\0') // Checking if equal to end of typed input
			UserPassword[i] = tolower(UserPassword[i]); // If not, capitilize it
		else
			break; // End of typed input, quit searching

	cout << "\n\n\n" << UserPassword << "\n\n\n";
	return 0;
}

int variableGrab()
{
	for (int i = 0; i < 2; i++)
	{
		if (i == 0)
		{
			test1 = passwordArray[i];
		}
		else if (i == 1)
		{
			test2 = passwordArray[i];
		}
		else
		{
			return 0;
		}
	}
	return 0;
}
Last edited on
That still doesnt compile. Include files are missing, and variables(test1, test2, and the array itself) are missing. Can you provide it?

You should make the function variableGrab into a void function. You are not using the return value so it shouldnt be an integer function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void variableGrab()
{
	for (int i = 0; i < 2; i++)
	{
		if (i == 0)
		{
			test1 = passwordArray[i];
		}
		else if (i == 1)
		{
			test2 = passwordArray[i];
		}
	}
}


Edit: You can also change this - int variableGrab(); into this - variableGrab();
Last edited on
I have updated the previous comment with all includes and variables, its all part of a bigger project, but I am currently running this section from within a menu, so any and all data passed in and out of these functions is exclusive to this code.

I made it a void and removed the returns but now I have a "string subscript out of range" error
You should learn how to debug to save yourself countless of wasted hours on small problems (and countless of sleepless nights in the future).

passwordArray = buffer.str();

After this, passwordArray is "" (I just mention that it's a string, not an array, variable names are important). You just create buffer, and then do nothing with it? What do you expect to happen. It's an empty string.

If you do this - passwordArray = "12"; instead, test1 and test2 will output 49 and 50.

Also, you're couting test1 twice, instead of test2.
Last edited on
Apologies, I must of made a mistake when transferring it over from another program last night, the for loop that initiates returnVal(); is where buffer is used, so instead of

 
cout << returnVal(s[i]);


its

 
buffer << returnVal(s[i]);


So that once that is completed, buffer is passed into passwordArray

Why are the outputs 49 and 50? is it because its grabbing the ASCII decimal value not the actual ASCII character?
Yes.

http://www.asciitable.com/index/asciifull.gif

You can see that here.

What exactly do you want the output to be?
Last edited on
test1 = "3"
test2 = "1"

as it is in the passwordArray string, or as I'll now rename it passwordString

okay, it now works as you've details, when I input the word "carrot" using the cin, the returnVal(); function converts it to "3118181520" as it should and passed it to passwordString

and the variableGrab(); function inputs the 2 values to test 1 and 2, but it outputs as

test1 = "51"
test2 = "49"
I seem to have solved it by adding a -48 when the values are passed to test(1/2)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void variableGrab()
{
	for (int i = 0; i < 2; i++)
	{
		if (i == 0)
		{
			test1 = (int)passwordArray[i]-48;
			
		}
		if (i == 1)
		{
			test2 = (int)passwordArray[i]-48;
		}
		
	}
}

Well. The first 2 values are 3 and 1. If you look at the table, the decimals for it are 51 and 49, so the output is correct. Are you expecting something else? Because then you'd be doing it the wrong way.
Well I was hoping it would literally copy the char from the string, could this be solved by changing variable types?
Last edited on
If you changed test1 and test2 from integer to char, then yes test1 would be 3 and test2 would be 1 assuming you enter carrot as your password.
Last edited on
If it's not a silly question, why is there a loop at all here:
1
2
3
4
5
6
7
8
9
10
11
12
13
	for (int i = 0; i < 2; i++)
	{
		if (i == 0)
		{
			test1 = (int)passwordArray[i]-48;
			
		}
		if (i == 1)
		{
			test2 = (int)passwordArray[i]-48;
		}
		
	}

Why not instead:
1
2
    test1 = passwordArray[0]-'0';
    test2 = passwordArray[1]-'0';
Because my eventual plan, was to use this loop multiple times

Test1 and test2 were to be changed and be included in providing variables for another function.

I intended to run this function repeatedly until there were no more character or not enough characters in passwordArray (which is now passwordString)

It's a very very very simplistic encryption key derived from a user input.
maybe define the function something like this:
1
2
3
4
5
6
7
8
9
10
11
12
void variableGrab(int & test1, int & test2, int i)
{
    if (i*2 < passwordArray.size())
        test1 = passwordArray[i*2] - '0';
    else
        test1 = -1;

    if (i*2+1 < passwordArray.size())
        test2 = passwordArray[i*2+1] - '0';
    else
        test2 = -1;
}


Here, provided i is with the size limits, a pair of values test1 and test2 will be returned from the function for a particular value of i which is controlled outside this function.

Notice I used three parameters in the function, I gather that you have been using lots of global variables, that's a bad practice which can lead to uncontrolled and hard to understand code, since it is never quite clear which function might be using which variable and for what purpose.

edit: following my own advice, passwordArray should also have been a parameter.
Last edited on
Topic archived. No new replies allowed.