Create Array of Int - C++

Pages: 12
Hello,

I'd like to get a string from input and create an array of int with my name.

Severity Code Description Project File Line Suppression State
Error C2040 'myname': 'int [10]' differs in levels of indirection from 'std::string' C++_6 C:\Users\My\source\repos\C++_6\C++_6\Source.cpp 9


1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

int main()
{
	string myname;
	cin >> myname;

	int myname[10];


}
you cannot have 2 variables with the same name in the same block of code.
myname can be a string, or an integer array, but it can't be both at once in this language.

not sure what you want to DO, but, characters ARE integers, and strings ARE arrays of characters (sort of).

if you remove line 9,
myname[0] is an integer/character, in other words. perhaps it is 'S' for shervan, which is 83.
Last edited on
I'm not sure what you're after but as jonnin says above - characters are integers. Possibly something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>

int main()
{
	constexpr size_t maxArr {20};

	int namint[maxArr] {};
	size_t nochars {};
	std::string myname;

	std::getline(std::cin, myname);

	for (; nochars < maxArr && nochars < myname.size(); ++nochars)
		namint[nochars] = myname[nochars];

	for (size_t i = 0; i < nochars; ++i)
		std::cout << namint[i] << ' ';

	std::cout << '\n';
}



seeplus
115 101 101 112 108 117 115


But also:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>

int main()
{
	std::string myname;

	std::getline(std::cin, myname);

	for (const auto ch : myname)
		std::cout << static_cast<int>(ch) << ' ';

	std::cout << '\n';
}


which produces the same output.
Hello Shervan360,

See if this is what you want:
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
#include <iostream>
#include <string>

//using namespace std;  // <--- Best not to use.

int main()
{
    constexpr unsigned MAXSIZE{ 10 };

    int arrName[MAXSIZE];
    std::string strName;

    std::cout << "\n Enter your name (no spaces): ";  // <--- The "cin" ALWAYS needs a prompt.
    std::cin >> strName;

    std::cout << "\n ";

    for (size_t idx = 0; idx < strName.size(); idx++)
    {
        arrName[idx] = strName[idx];

        std::cout << (idx ? ", " : "") << arrName[idx];
    }

    return 0;  // <--- Not required, but makes a good break point for testing.
}


Output might be:

 Enter your name (no spaces): ABCDE

 65, 66, 67, 68, 69



Andy
Last edited on
@Handy Andy,
what happens when the user enters Ann-Kathrin for the name?
What's supposed to happen? Works OK as there's no space in the name. The issue with Andy's code is if the name contains a white-space char - but the prompt says no spaces.



 Enter your name (no spaces): Ann-Kathrin

 65, 110, 110, 45, 75, 97, 116, 104, 114, 105, 110


PS. There's a buffer overun issue though, as L18 should also check idx against MAXSIZE. or L14 limit the input size

 
std::cin >> std::setw(MAXSIZE) >> strName;

Last edited on
@thmm,

Entering "Ann-Kathrin" will work until you try to change the characters into ints then you would over-run the array size.

I was more concerned about the different variable names to start with and was hoping to get a response form OP, but if you feel that writing the complete correct code is better then go ahead.

Andy
@seeplus, @Handy Andy,

maybe I am blind, but arrName has a size of 10 and Ann-Kathrin is 11 chars.
Shouldn't there be ann array overflow?

Edit:
VS 2019 actually confirms it: Run-Time Check Failure #2 - Stack around the variable 'arrName' was corrupted.
Last edited on
and Ann-Kathrin is 11 chars

It's 12 if you count the terminating '\0'.
It's 12 if you count the terminating '\0'.

I thought that a std::string doesn't count the terminating '\0'
Presumably you are talking about arrName, which is a c-style string and is currently sized at 10.
A std::string cannot (in normal circumstances) overflow.

Note that the code doesn't properly zero-terminate the c-string string. It should be more like:

1
2
3
4
    for (size_t i = 0; i < strName.size(); ++i)
        arrName[i] = strName[i];
    arrName[strName.size()] = '\0';
    std::cout << arrName << '\n';

Last edited on
arrName is an array of unsigned int.

1
2
3
constexpr unsigned MAXSIZE{ 10 };
int arrName[MAXSIZE];
std::string strName;


Last edited on
Oh, I didn't notice that. Forget it then. I made the mistake of not reading the original post, and not reading the code very closely either.
Due to variable length of an inputted name the size of the int array should be larger than the largest reasonable name. Including spaces and non alphabetic characters.

The int array should be declared as unsigned since characters can't be less than zero.

Also, if you use C++ strings you should include the <string> header.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string> // if you use a C++ string you need to include the C++ header

int main()
{
   std::cout << "Enter your name: ";
   std::string theName;
   std::getline(std::cin, theName);  // retrieve the entire line of text
   std::cout << '\n';

   const unsigned MAXSIZE { 100 }; // declare a size larger than the largest reasonable size for a name.

   unsigned theNameConverted[MAXSIZE];

   for (size_t i { }; i < theName.size(); ++i)
   {
      theNameConverted[i] = static_cast<unsigned>(theName[i]); // convert each character to unsigned int

      std::cout << theNameConverted[i] << ' ';  // display each int
   }
   std::cout << '\n';
}
Enter your name: Joe d'Ragman

74 111 101 32 100 39 82 97 103 109 97 110


Since std::strings are being used, why use C-style/regular arrays? Declaring the size of a std::vector can be based on the size of the name:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <vector>

int main()
{
   std::cout << "Enter your name: ";
   std::string theName;
   std::getline(std::cin, theName);
   std::cout << '\n';

   // declare the vector's size dependent on the size of the string
   std::vector<unsigned> theNameConverted(theName.size());

   for (size_t i { }; i < theName.size(); ++i)
   {
      theNameConverted[i] = static_cast<unsigned>(theName[i]);

      std::cout << theNameConverted[i] << ' ';
   }
   std::cout << '\n';
}
Enter your name: Joe d'Ragman

74 111 101 32 100 39 82 97 103 109 97 110


If'n you want to use a regular array consider declaring it on the heap:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>

int main()
{
   std::cout << "Enter your name: ";
   std::string theName;
   std::getline(std::cin, theName);
   std::cout << '\n';

   // declare the array on the heap
   unsigned* theNameConverted = new unsigned[theName.size()];

   for (size_t i { }; i < theName.size(); ++i)
   {
      theNameConverted[i] = static_cast<unsigned>(theName[i]);

      std::cout << theNameConverted[i] << ' ';
   }
   std::cout << '\n';

   // it pays to be neat, delete the array's heap memory.
   delete[] theNameConverted;
}
Enter your name: Lloyd Schmuckatelli

76 108 111 121 100 32 83 99 104 109 117 99 107 97 116 101 108 108 105
Since when in a strictly typed language like C++ are characters integers?
Since when in a strictly typed language like C++ are characters integers?

See [basic.fundamental]
Types bool, char, wchar_­t, char8_­t, char16_­t, char32_­t, and the signed and unsigned integer types are collectively called integral types. A synonym for integral type is integer type.

https://eel.is/c++draft/basic.fundamental#11
Last edited on
Quoting further from the same source which addresses the original point that they aren’t the same.
The types described in this subclause are called fundamental types.
[Note 10: Even if the implementation defines two or more fundamental types to have the same value representation, they are nevertheless different types. — end note]
My emphasis.
We'll just agree to disagree.

To recap, the claim was "characters are integers".
Last edited on
Why are you arguing? characters are obviously not integers. The need for casting, by definition, proves they aren’t and your own reference if you had bothered to read it confirms it. There’s no agreement to disagree on facts. You’ve chimed in without reading your own material properly.
The type char is an integer type.
We can check it with std::numeric_limits<char>::is_integer https://eel.is/c++draft/numeric.limits.members#18

#include <iostream>
#include <type_traits>
#include <limits>

1
2
3
4
5
6
7
8
int main()
{
    const auto yes_or_no = [] ( bool f ) -> const char* { return f ? "yes" : "no" ; } ;

    std::cout << std::boolalpha << "type char:\n---------\n"
              << "            is it an integral type? " << yes_or_no( std::is_integral<char>::value ) << '\n'
              << "  is it an integer arithmetic type? " << yes_or_no( std::numeric_limits<char>::is_integer ) << '\n' ;
}

g++ -std=c++20 -O3 -Wall -Wextra -pedantic-errors -pthread main.cpp -lstdc++fs && ./a.out
echo && echo ===== && echo  
clang++ -std=c++17 -stdlib=libc++ -O3 -Wall -Wextra -pedantic-errors -pthread main.cpp -lsupc++ -lstdc++fs && ./a.out
type char:
---------
            is it an integral type? yes
  is it an integer arithmetic type? yes

=====

type char:
---------
            is it an integral type? yes
  is it an integer arithmetic type? yes

http://coliru.stacked-crooked.com/a/fd5ffabedb3e4a53
Pages: 12