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

  • Context: Comp Sci 
  • Thread starter Thread starter sdoug041
  • Start date Start date
  • Tags Tags
    Arrays C++ Printing
Click For Summary

Discussion Overview

The discussion revolves around the challenge of printing integers stored in a character array as a string in C++. Participants explore the use of format specifiers in print statements and the context of a specific programming assignment involving ciphering techniques.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant expresses difficulty in printing integers as a string using the %s format specifier, leading to program crashes.
  • Another participant questions the rationale behind storing numbers in character arrays and using %s for printing.
  • It is suggested that printing integers one at a time in a loop may be a more appropriate approach.
  • A participant clarifies that the assignment involves creating a program for ciphering, which requires converting characters to integers and back to characters for encryption purposes.
  • Several participants request more specific information or code snippets to better understand the issue and provide assistance.

Areas of Agreement / Disagreement

Participants do not appear to reach a consensus on the best approach to the problem, and multiple viewpoints regarding the use of character arrays and format specifiers remain present.

Contextual Notes

Participants note the need for additional context, such as specific integer values and code snippets, to diagnose the crashing issue effectively. There is an implied complexity in converting between character and integer representations in the context of the ciphering assignment.

Who May Find This Useful

This discussion may be useful for individuals working on C++ programming assignments involving character arrays, format specifiers, and encryption algorithms.

sdoug041
Messages
25
Reaction score
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
Why are you storing numbers in character arrays? Why are you trying to print numbers with %s?

- Warren
 
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.
 
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.
 
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
 
What values do these integers take?
 
We help much unless we see the code you used.
 
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:
 
Help us help you help us help us all.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
Replies
9
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
22
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
Replies
7
Views
3K
  • · Replies 13 ·
Replies
13
Views
3K