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...

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.