Converting a const string into an array of ints? - c++

Hello I am trying to convert a const character string into an array of ints but when I try it does not allow it. My code is:
int isRegistered(const char str[]) {
int isbnInt[10], i;
//char isbnArray[10];
//isbnArray = str; ----> something I tried
for (i = 0; i < 10; i++)
{
isbnInt[i] = atoi(str[i]);
cout << isbnInt[i] << endl;
}
}
But when I try to compile it, I get an error saying "invalid conversion from char to const char*"

atoi call expects a const char * arguement, while you pass a char, this is the problem.
You can just do the below to convert the character to number. This subtracts the ascii value of 0 from the character itself ( since 0-9 are sequentially increasing in the ascii code.)
isbnInt[i] = str[i] - '0';

Try:
for (i = 0; i < 10; i++)
{
isbnInt[i] = str[i] - '0';
cout << isbnInt[i] << endl;
}
atoi takes const char* as input instead of single char.

Your code could also be written:
for (i = 0; i < 10; i++)
{
char foo = str[i];
isbnInt[i] = atoi(foo);
cout << isbnInt[i] << endl;
}
Which won't work (as you've found); atoi expects a char*, not a char.
Try:
int isbm = atoi(str);
and see if that does what you wanted.

Related

If a string is an array of char, how do you convert it into an array of interger

I kept getting an error with this loop. If there are something i missed, please help. Thank You!
int main(){
string hasil;
int cod[5];
hasil = "99999";
for(int i = 0; i < 5; i++){
cod[i] = stoi(hasil[i]);
}
for(int i = 0; i < 5; i++){
cout << cod[i] + 1;
}
std::stoi() takes a std::string, not a char. But std::string does not have a constructor that takes only a single char, which is why your code fails to compile.
Try one of these alternatives instead:
cod[i] = stoi(string(1, hasil[i]));
cod[i] = stoi(string(&hasil[i], 1));
string s;
s = hasil[i];
cod[i] = stoi(s);
char arr[2] = {hasil[i], '\0'};
cod[i] = stoi(arr);
stoi is for converting entire strings to integers, but you're only giving it single characters.
You could either build strings from each character like so:
cod[i] = std::stoi(std::string(1, hasil[i])); // the 1 means "repeat char one time"
Or calculate the actual integer yourself using a bit of ascii math (assuming everything is a valid digit):
cod[i] = hasil[i] - '0'; // now '0' - '0' returns 0, '5' - '0' returns 5, etc...
The below complete working program shows how you can achieve what you want:
#include <iostream>
int main(){
std::string hasil;
int cod[5];
hasil = "99999";
for(int i = 0; i < 5; ++i)
{
cod[i] = hasil[i] - '0';
}
for(int i = 0; i < 5; i++){
std::cout << cod[i];
}
return 0;
}
The output of the above program can be seen here.
std::stoi only accpet std::string or std::wstring as an argument type. But hasil[i] is a char
In C++, '0' to '9' is guarantee to be ascending in ACSII values, so, you can do this:
cod[i] = hasil[i] - '0';

C++ outputs strange characters instead of numbers (Windows)

I need to program a lotto generator for my education that will randomly roll numbers and check for duplicate entries and replace them otherwise. When I start the program there are no error messages and the program runs but I only see strange characters instead of numbers. A picture of the problem
What is wrong with my code?
#include <iostream>
#include <array>
#include <time.h>
std::array<unsigned char, 6> lottoZahlen = {0, 0, 0, 0, 0, 0};
void arrayFuellen();
unsigned char checkDuplikate(unsigned char);
void arraySortieren();
int main()
{
arrayFuellen();
arraySortieren();
std::cout << "\n---- Ihre Glueckszahlen lauten: ----" << std::endl;
for (unsigned char lottoGlueck : lottoZahlen)
{
std::cout << lottoGlueck << std::endl;
}
std::cout << "---- Glueckszahlen Ende ----" << std::endl;
}
void arrayFuellen()
{
srand(time(NULL));
unsigned char wuerfelZahl = 0;
unsigned char wuerfelZahlChecked = 0;
for (unsigned char i = 0; i < sizeof(lottoZahlen); i++)
{
wuerfelZahl = rand() % 45 + 1;
wuerfelZahlChecked = checkDuplikate(wuerfelZahl);
lottoZahlen[i] = wuerfelZahlChecked;
}
}
unsigned char checkDuplikate(unsigned char checkZahl)
{
srand(time(NULL));
bool dublette = false;
do
{
dublette = false;
for (unsigned char j = 0; j < sizeof(lottoZahlen); j++)
{
if (checkZahl == lottoZahlen[j])
{
checkZahl = rand() % 45 + 1;
dublette = true;
}
}
} while (dublette);
return checkZahl;
}
void arraySortieren()
{
unsigned char merker = 0;
bool vertauscht = false;
do
{
vertauscht = false;
for (unsigned char i = 1; i < sizeof(lottoZahlen); i++)
{
if (lottoZahlen[i - 1] > lottoZahlen[i])
{
merker = lottoZahlen[i];
lottoZahlen[i] = lottoZahlen[i - 1];
lottoZahlen[i - 1] = merker;
vertauscht = true;
}
}
} while (vertauscht);
}
"char" is a type that is used to store characters, and the output stream will interpret it as such in your for-loop. So if you have value 65, it will actually be displayed as a capital A (which has ASCII value 65). To display numbers, you should use a type that the output stream recognizes as a number, such as "int".
There are several ways of doing what you want, printing char as integer/decimal value:
using casging int():
std::cout << int(lottoGlueck) << "\n";
using good old (C style) printf(), some would say do not use this, but there are advantages and disadvantages to using printf().
printf("%d\n", lottoGlueck);
As suggested, you can use std::to_string(), I personally do not recommend this for printing a single character, simply because it converts a character to a string to print out an integer.
In production code I use number 1, in debugging I use 2. There are disadvantages/advantages to using both, but you can read this to better understand those.
When it comes to pinging strings as decimal values, you have std::to_string() and also std::cout << std::dec << string << "\n".
you are printing non printable characters:
https://upload.wikimedia.org/wikipedia/commons/d/dd/ASCII-Table.svg
the ones between [] are not printable characters.
if you write: int i = 5 and then std::cout << i
it will print the corresponding character, with value 5. But the value 5 is not the character '5', so if you expect it to be a printable number, you need to convert it:
std::cout << std::to_string(i)
(not sure if this was your intention though :) )
In addition to the answers to your question, you can check whether your value is printable or not by using isprint().
std::cout << isprint(lottoGlueck) << std::endl;
This will print 0 (false) if your value is non-printable.

strcat Function in c++

I'm new to C and C++ programming, can anyone give me a hint on what I'm doing wrong here. I'm trying to write to concat function that takes to pointers to chars and concatenates the second to the first. The code does do that, but the problem is that it adds a bunch of junk at the end. For instance, when passing the arguments - "green" and "blue", the output will be "greenblue" plus a bunch of random characters. I also wrote the strlen function that strcat uses, which I will provide below it for reference. I'm using the online compiler at https://www.onlinegdb.com/online_c++_compiler
The exact instructions and specification is this:
The strcat(char *__s1, const char *__s2) functions concatenates the contents of __s2 onto __s1 beginning with the NULL character of __s1. Note: The concatenation includes the NULL character of __s2. The function returns __s1.
int main(int argc, char** argv)
{
const int MAX = 100;
char s1[MAX];
char s2[MAX];
cout << "Enter your first string up to 99 characters. ";
cin.getline(s1, sizeof(s1));
int size_s1 = strlen(s1);
cout << "Length of first string is " << size_s1 << "\n";
cout << "Enter your second string up to 99 characters. ";
cin.getline(s2, sizeof(s2));
int size_s2 = strlen(s2);
cout << "Length of second string is " << size_s2 << "\n";
cout << " Now the first string will be concatenated with the second
string ";
char* a = strcat(s1,s2);
for(int i = 0; i<MAX; i++)
cout <<a[i];
// system("pause");
return 0;
}
//strcat function to contatenate two strings
char* strcat(char *__s1, const char *__s2)
{
int indexOfs1 = strlen(__s1);
int s2L = strlen(__s2);
cout <<s2L << "\n";
int indexOfs2 = 0;
do{
__s1[indexOfs1] = __s2[indexOfs2];
indexOfs1++;
indexOfs2++;
}while(indexOfs2 < s2L);
return __s1;
}
//Returns length of char array
size_t strlen(const char *__s)
{
int count = 0;
int i;
for (i = 0; __s[i] != '\0'; i++)
count++;
return (count) / sizeof(__s[0]);
}
The behavior you are seeing is a result of the null terminator of __s1 being overwritten by data from __s2 and no new null terminator being appended. The extra characters you are seeing are just random values in RAM that happen to be after the end of your string. To prevent this a NULL character MUST be added at the end of your string.
A working version is as follows:
char* strcar(char *__s1, const char *__s2)
{
//check inputs for NULL
if(__s1 == NULL || __s2 == NULL)
return NULL;
int s1Length = strlen(__s1);
int s2Length = strlen(__s2);
//ensure strings do not overlap in memory
if(!(__s1 + s1Length < __s2 || __s2 + s2Length < __s1))
return NULL;
//append __s2 to __s1
//the "+ 1" here is necessary to copy the NULL from the end of __s2
for(int i = 0; i < s2Length + 1; i++)
result[s1Length + i] = __s2[i];
}
You Need to add a trailing "\0"-char at the end of __s1.
e.g. insert
__s1[indexOfs1] = 0;
before your return-line.

Trimming start of Cstring without copying

I managed to get my homework to work but It shouldn't work because i have not finished it. I don't know why it does. I need help.
#include<iostream>
using namespace std;
char* trim(char* str) {
const int lenStr = strlen(str);
int characters = 0;
bool trimmableFront = false;
int firstChar;
//check if trimmableFront + location of first char
for (int i = 0; i < lenStr; i++) {
if (*(str + i) != ' ') {
if (characters == 0)
firstChar = i;
characters++;
}
if (characters == 0) {
trimmableFront = true;
}
}
//trim Front //THIS PART SHOULD BEHAVE DIFFERENTLY
if (trimmableFront) {
for (int i = 0; i < lenStr; i++) {
if((firstChar + i <= lenStr))
*(str + i) = *(str + firstChar + i);
}
}
return str;
}
int main() {
char str[] = " why does it work?";
trim(str);
cout<< str <<endl;
return 0;
}
At the end of trim(*char) function, trimmed string should have still leftovers from previous locations.
For some reason it is perfectly trimmed and works as intended printing "why does it work?" but it should print something like "why does it workt work?"
The reason why it works is because as you trim the string by shifting each character you also shift the terminating null character '\0'. As you probably know c-strings are array of characters terminated by '\0', so as you print str with cout all characters are printed until the null value is reached: that is way the leftovers are not printed.

How do I print the elements of a char array?

I have to convert a decimal value into a string that shows the binary value, e.g. given 8, I need to print a string "1000". I have the conversion from decimal to binary, but when I print the values directly form the char array, I get little question marks instead of numbers. I know it has something to do with the way char arrays read values, but I can't figure out how to correct the issue.
void dec2Bin(int value, char binaryString[]) {
int remainder = 0;
int binDigit = 0;
int i = 0;
while (value != 0) {
binDigit = value % 2;
value /= 2;
binaryString[i] = char(binDigit);
i++;
}
for (int k = i - 1; k > 0; k--) {
cout << binaryString[k];
}
}
int main()
{
cout << "Enter a decimal number: ";
int num;
cin >> num;
char binaryString[20] = "";
dec2Bin(num, binaryString);
return 0;
}
When you do
binaryString[i] = char(binDigit);
you are assigning the decimal value 0 or 1 to binaryString[i]. That's okay, a char is basically nothing more than a small integer.
The problems comes when you want to print the value, as the only overloaded << operator to handle char treats the characters as a character, and in most encodings the values 0 and 1 are not printable.
There are two solutions:
Either you convert the character you want to print into a larger integer which won't be treated as a character:
cout << static_cast<int>(binaryString[k]);
Or you make the array contain actual printable characters instead:
binaryString[i] = binDigit + '0';