How can I print integers as a string in C++ using character arrays?

In summary, the problem is that Warren is trying to print the integers as characters, but he is not able to do so using %s because the characters in the character arrays are stored in integer values. He needs to convert the characters into their corresponding letter values in order to be able to print them. When he tries to do this, he crashes his program.
  • #1
sdoug041
26
0

Homework Statement



I have a bunch of integers stored in a character array, and I am trying to print the integers as characters. I can print them as characters when I use %c in the print statement, but the problem is I need to print them as a string. My program crashes when I attempt to use the %s in the print statement. Can anyone help me out?


Homework Equations





The Attempt at a Solution

 
Physics news on Phys.org
  • #2
Why are you storing numbers in character arrays? Why are you trying to print numbers with %s?

- Warren
 
  • #3
I would just print them one at a time in a for loop. The %s format specifier is only for zero-terminated arrays of type char.
 
  • #4
Sorry I should have been more specific; the assignment I am working on is to develop a program that can cipher and decipher vigener's/ceaser ciphering.

My function I have created receives three references of character arrays. One array stores the key word, one array stores the secret message, and one array is empty, waiting to be filled with the encrypted text; which is the job of this function.

I had to convert the characters stored in the two arrays into their corresponding integer values in order to develop the encrypted values (using a mathematical equation) which should then be stored into the empty character array. All the values in this array correspond to letter characters which should spell out the encrypted text.

The only problem I have is being able to convert these values back to their corresponding letter characters. I hope this clears up the problem.
 
  • #5
Can you ask a more specific question, or show us the relevant parts of your code, so that we can understand where it's crashing?

- Warren
 
  • #6
What values do these integers take?
 
  • #7
We help much unless we see the code you used.
 
  • #8
Lancelot59 said:
We help much unless we see the code you used.
And if we do see the code, we don't help at all! :wink:
 
  • #9
Help us help you help us help us all.
 

1. What is a character array in C++?

A character array in C++ is a data structure that stores a sequence of characters in a contiguous block of memory. It is similar to a string, but unlike strings, character arrays are mutable and have a fixed size.

2. How do you declare a character array in C++?

To declare a character array in C++, you can use the following syntax: char arrayName[arraySize]; This creates an array of characters with the specified size.

3. How do you print a character array in C++?

To print a character array in C++, you can use the cout object from the iostream library. For example, if your character array is named myArray, you can print it using cout << myArray;

4. How do you get the length of a character array in C++?

To get the length of a character array in C++, you can use the strlen() function from the cstring library. This function takes a character array as an argument and returns the number of characters in the array.

5. Can you change the size of a character array in C++?

No, the size of a character array in C++ is fixed at the time of declaration and cannot be changed. If you need a dynamic data structure that can change in size, you can use a std::vector instead.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
943
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Programming and Computer Science
Replies
1
Views
531
  • Engineering and Comp Sci Homework Help
Replies
22
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
Back
Top