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>
usingnamespace 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.
#include <iostream>
#include <string>
//using namespace std; // <--- Best not to use.
int main()
{
constexprunsigned MAXSIZE{ 10 };
intarrName[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
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
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.
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';
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.
#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';
constunsigned 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:
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.
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]
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.
int main()
{
constauto yes_or_no = [] ( bool f ) -> constchar* { 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