array

make and run a program that will accept a user inut not more than 80 chracters and find the total number of inputted character..
Last edited on
closed account (28poGNh0)
would you mind do some effort by giving a shot in your own code?
This program is easy and I think you can do it by yourself and we're all here for help
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
int J;
cout<<"Enter a Character: \n";
char Letter[80];
cin>>Letter;
J=strlen(Letter);
cout<<"You Entered: "<<J<<"\n";
if (J>80)
cout<<"\nThe Letter you entered is greater than 80:\n ";

system("pause");
return 0;
}
this is right?
Does it compile without errors and warnings?
Does it run as expected?

I bet not.

If you don't know how much input there will be, then you cannot statically reserve X bytes for it and still potentially read more than X. "accept a user inut not more than 80 chracters"

Overall, standard library containers are much easier than C-style arrays. std::string.

cin>> is peculiar about what it does. (Not really, it is very logical, but before you learn it, it will surprise you.) Read it's reference documentation. std::getline and std::get are valid alternatives to consider.
Last edited on
it compile without errors and warning..
and yes it run as expected.
but.. when i type more than 80 character .will appear you enter character is greater than 80. it wrong ryth? because when i type more than 80 it appears invalid..
closed account (28poGNh0)
The condition
1
2
if (J>80)
cout<<"\nThe Letter you entered is greater than 80:\n ";

is protecting your program for overriding the array
because when you try to get more than 80 characters is like you try to put your feet in one shoe ^_^
Last edited on
That protects nothing. If it manages to print out before the program crashes, then it merely informs the user that irreversible mistake has already rendered the program to undefined state.
I would simply declare the array with 81 and once it reaches the 81st print out that you exceed the amount of 80 Chars and either let the user write again from [0] or simply print your msg and close it.

Easiest solution imho.
Last edited on
Topic archived. No new replies allowed.