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];
}
elseif (i == 1)
{
test2 = passwordArray[i];
}
else
{
return 0;
}
}
return 0;
}
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?
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
#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 (unsignedint 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
elsebreak; // 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];
}
elseif (i == 1)
{
test2 = passwordArray[i];
}
else
{
return 0;
}
}
return 0;
}
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.
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?
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
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.
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.