C-strings

Help pls with this task.
Input a string and output ANSI-codes of digits in this string

Nice little task.
What's the question ?
Hello toradora,

Do you mean something like this:
1
2
3
4
5
6
7
8
9
10
char str[]{ "ABCDEFG" };

for (char it : str)
	std::cout << "\n " << it << " = " << static_cast<int>(it);

 A = 65
 B = 66
 C = 67
 D = 68
 E = 69
 F = 70
 G = 71
 
 Press Enter to continue:


Hope that helps,

Andy

Edit: Should have put the output earlier.
Last edited on
It's his homework you really should encourage him to try it on his own.. Maybe he just didn't know how to convert a string literal to its equivalent ASCII.

I bet he will paste that as his homework and then get caught because you put static caste.. Lol

By the way (int) 'string literal' will give you integer equivalent of the string literal also. Use what you have been taught.
Last edited on
@Nwb,

I bet he will paste that as his homework and then get caught because you put static caste.. Lol
This may be true, but between the range for loop and the "static_cast" the point was to give an idea.

By the way (int) 'string literal' will give you integer equivalent of the string literal also. Use what you have been taught.
You assume I have been taught to use "static_ cast". This is what I have learn over the last two years here and the fact that I am trying to learn and use the C++11 standards.

I found something after I wrote and tested this which became an unintentional design flaw. In the "str" array how many elements are there? And what output do you actually get? Something that needs fixed before it is finished along with possibly changing the for loop.

It is a starting point, but something that needs to be finishes, so as for homework it still has a use.

Andy
@tpb,

Thank you I will keep that in mind.

Being the "Beginners forum" I try to explain in simple terms what others say or post with their code.

I still consider myself a beginner, so I can understand what some of these people are trying to understand.

At least until someone corrects me on something I do not quite understand fully.

Andy
closed account (1vRz3TCk)
It is pointless to respond to...
but unfortunately sometimes you have to to correct misinformation.

(And casting a string literal to an int? What?)
Just think that needs hi-lighting a bit more.
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main()
{
   const char a[] = "ABCDE12345";

   for ( char c : a ) cout << c << ' ';
   cout << '\n';

   for ( int  c : a ) cout << c << ' ';
   cout << '\n';
}


A B C D E 1 2 3 4 5   
65 66 67 68 69 49 50 51 52 53 0
Last edited on
(And casting a string literal to an int? What?)
Just think that needs hi-lighting a bit more.

What's wrong with typecasting char to an int? I read what I said again, and tpb deleted his post so I don't know what he said. *shrugs*

You assume I have been taught to use "static_ cast". This is what I have learn over the last two years here and the fact that I am trying to learn and use the C++11 standards.

I found something after I wrote and tested this which became an unintentional design flaw. In the "str" array how many elements are there? And what output do you actually get? Something that needs fixed before it is finished along with possibly changing the for loop.

It is a starting point, but something that needs to be finishes, so as for homework it still has a use.

Andy


Andy that wasn't for you that was for OP, I told him to use what he had been taught, my implication was that he should use typecasting if he had been taught that, you'd get caught if you use something you haven't been taught yet.

Granted that your code is just to give an idea, but if he doesn't know what a ranged for loop is, what static caste is, then he wouldn't understand the snippet itself. But since it works he would just copy paste it as his homework. But that's my perspective anyway..
Last edited on
@Nwb,
You confused a "string literal" with a "char" - therein lies the problem that caused some rolling eyes. For "string literal" see a straightforward explanation at
http://www.cplusplus.com/forum/beginner/33483/#msg180026
and the comprehensive version at
https://en.cppreference.com/w/cpp/language/string_literal


What @Handy Andy wrote was just fine; there are many ways of casting and that was the least of the issues. Both he (by code) and @Thomas1965 (by question) tried to get clarification from the original poster as to what was required.


@toradora,
You are going to have to clarify your question:
Input a string
C-string or std::string ?
ANSI-codes
Depends on you using a particular character set. ANSI is one extended (256 characters) version of ASCII (128 characters), and may or may not be the native character-collating sequence for your implementation.
of digits
Did you mean that? "Digits"->"numbers". Or did you just mean "characters" or "letters"?

If you posted some of your own code we might be able to work that out.

Last edited on
@lastchance

I thought 'string literal' was singular and 'string' was plural.. no .-.? Thanks anyway but it makes sense now - what the confusion was.
Nwb wrote:
I thought 'string literal' was singular and 'string' was plural.. no .-.?

No.
Topic archived. No new replies allowed.