how to make a my program to finish repeated the loop when the user enter the sentinel value?? for example the "-1"...

this is my attempt..but still something wrong here..

#include <iostream>
using namespace std;

int printreverse(char letter_box[],int i)
{
   for (int x=i; x>=0; x--){
       cout<<letter_box[x];
   }                       
}                        

    char letter_box[10];
char answer;
int main ()
{
   const int SENTINEL=-1; 
   int i=0;
   
do {
    cout<<"Enter any letter (maximum is 10) :";

    cin>>letter_box[i];
    
    i++;
}while ( !=SENTINEL); //how do i correct this????


printreverse(letter_box,i);

system("pause");
}

Thank You for helping...

Dani AI

Generated

A focused correction and checklist for the original task (read up to ten letters into an array, stop on a period, then print them in reverse). Key fixes: use a char sentinel '.' (not -1), never allow the array to overflow, do not store the sentinel character, and make the reverse routine accept the count of stored letters (it should be void if it only prints). 's warning about comparing a char to -1 is correct (char signedness varies). was right to stress a bound check (i < 10). 's points about using '.' as the sentinel, cin.ignore() when mixing input methods, and avoiding printing the sentinel are also important.

const int MAX = 10;
const char SENTINEL = '.';
char letter_box[MAX];
int i = 0;
char ch;

do {
    if (!(cin >> ch)) break;         // EOF or input error
    if (ch == SENTINEL) break;       // stop on '.'
    if (i < MAX) letter_box[i++] = ch;
    else break;                       // reached capacity
} while (true);

// print the stored letters (count = i) in reverse order
void printreverse(const char arr[], int count) {
    for (int k = count - 1; k >= 0; --k) cout << arr[k] << ' ';
    cout << '\n';
}

Notes and troubleshooting: cin >> ch skips whitespace, so input like a b c d. will work and the '.' will be read as a separate character. If preserving spaces is required, use cin.get() and handle the newline with cin.ignore() as suggested. Pass the stored count i to printreverse so the sentinel is never printed (this fixes the period-at-front problem). Also avoid system("pause") in portable code, and fix any function declared to return a non-void type if it doesn't actually return a value (the original printreverse was declared int but had no return).

Recommended Answers

All 4 Replies

while ( letter_box[i-1] !=SENTINEL); //how do i correct this???? char is just a one-byte integer, and has both negative and positive values.

When you say

cout<<"Enter any letter (maximum is 10) :";

Do you mean the maxiumum amount of letters you can enter is 10?
If so, your while loop should be

while(i<10)

so that after 10 letters is entered it exits the loop,
and if the user would want to exit early, like, after only 5 letters,
use

break;

this is the actual question :

Using functions,write a program that reads up toten letters into an array called letter_box[] and write the letters back to the screen in reverse order,For example,if the input is :

a b c d.

then the output should be :

d c b a

Use a period as sentinel value to mark the end of the input....

Your sentinel is given to you and is not a number so trying to compare your character to a number is not right. SENTINEL should be of type char and be equal to '.' It should become apparent based on what AD told you to what you should compare your SENTINEL value.

The second thing is, you have a second loop condition that you need to fulfill. When I first ran your program I could enter 11 characters or 11111 characters without any restriction. Nothing stopped me at 10.

Finally, you may want to look into using an ignore() call after you read in the character.

P.S., Purely an aesthetic thing, when you print out the reversed string you get the period at the beginning. So you should either pass i-1 to your method or adjust the method itself.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.