How to write a C++ program doing the following :
a series of names with no blank characters and displays the number of the names having more than ten charaters. the name must not exceed 25 characters and the series ends when the user enters the word "end".

Recommended Answers

All 3 Replies

#include<iostream>
#include<conio.h>
#include<string.h>
#define size 50     //minimum input 50
using namespace std;
int main()
{
    int i=0;;
string names[size];//store input
string names_dis[size];//save input condition fullfill
do
{
cout<<"Enter name"<<i+1<<"::"<<endl;
cin>>names[i];      
if(names[i]!="end"&&names[i].length()<=25&&names[i].length()>10)
{
names_dis[i]=names[i];  
}
i++;
}
while(names[i-1]!="end");
for(int j=0;j<i;j++)
{   
cout<<names_dis[j]<<endl;   
}

getch();    
}

You stated your objective and you've supplied code. However you haven't said what problem(s) you're having with your code.

#define size 50     //minimum input 50

You're asking for trouble #defining size to equal a number (or anything else). You got away with it here, but what if you had a program that used vectors and you wanted to use vector's size function?

vector<int> intVector;
cout << intVector.size();

Preprocessor changes "size" to "50", so the compiler has to try to make sense of

vector<int> intVector;
cout << intVector.50(); // error

It would be better to make the size upper-case if you're going to #define it. It's part of the naming convention to make it all upper-case anyway, even if it was a name like ABCDEFGHIJ that isn't going to be a common function name like "size". Constants and #define's are upper-case. Easier to read the code that way.

#define SIZE 50
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.