help!!!

the program runs without errors but after entering string n choice dere is no output and do u want to cont ques is asked

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
void main()
{ clrscr();
char ch,choice,str[50];
void upper(char []);
void lower(char []);
void non(char []);
void words(char []);

cout<<"*****MENU*****";
cout<<"\n1.No. of capital alphabets\n2.No. of small alphabets\n3.No. of non-alphabets\n4.No. of Words";
cout<<"\nenter choice:";
cin>>choice;
cout<<"enter a string:";
gets(str);
switch(choice)
{
case 1: upper(str);
break;
case 2: lower(str);
break;
case 3: non(str);
break;
case 4: words(str);
break;
default: "wrong choice";
break;
}

getch();
}

void upper(char ch[])
{
int count=0;
for(int i=0;ch[i]!='\0';i++)
if(isupper(ch[i]))
count++;
cout<<"No. of capital alphabets:"<<count;
}
void lower(char ch[])
{
int count=0;
for(int i=0;ch[i]!='\0';i++)
if(islower(ch[i]))
count++;
cout<<"No. of small alphabets:"<<count;
}
void non(char ch[])
{
int count=0;
for(int i=0;ch[i]!='\0';i++)
if(!isalpha(ch[i]))
count++;
cout<<"No. of non-alphabets:"<<count;
}
void words(char ch[])
{
int count=1;
for(int i=0;ch[i]!='\0';i++)
if(ch[i]==' ')
count++;
cout<<"No. of words:"<<count;
}



Please, use code tags and informative indentation. See http://www.cplusplus.com/articles/jEywvCM9/

You are using mainly C constructs, rather than C++.
You are using deprecated constructs.

The choice is a char. If you, as user, type 1, you are typing a character that looks like 1. It is not the same as integer value 1.

Your case statements should test '1' rather than 1.


Doesn't your compiler say anything about line default: "wrong choice";?
It should.
void main()

I'm sick of seeing this line of code in every single thread! Please update to a more recent compiler.
IceStorm wrote:
I'm sick of seeing this line of code in every single thread! Please update to a more recent compiler.

My advice to you would be, learn to give advice and politely recommend possible compilers rather than making a comment saying they need to update it. Unless otherwise stated you should always approach new users as complete beginners who may not know anything outside of what some outdated book or site may be teaching them.

@shrutika
As keskiverto pointed out you need to use code tags to help us read your code easier. As IceStorm pointed out, your code tells us you are using a compiler that is outdated (I'm guessing Bloodshed's Dev-C++ (comes with the old MinGW compiler of that time) -- which Bloodshed's Dev-C++ hasn't been updated since the first quarter of 2005). There is a gentleman who started updating Dev-C++ elsewhere that I would recommend getting instead -- http://orwelldevcpp.blogspot.com/ .

The things that make it obvious that you are using an outdated compiler is the header files (#include <iostream.h> ) that end with .h, but newer compilers no longer require .h (ie #include <iostream> ) for standard header files (user created headers require the .h at the end. The other thing, as IceStorm pointed out, is void main() because the C/C++ standards say main should return int int main(). Now embedded systems use void main(), but that is another topic altogether.

Lastly, I'd find a new learning source for learning C++ because if it is having you use that code, then it is also outdated. I'd recommend reading the tutorials on this site ( http://www.cplusplus.com/doc/tutorial/ ), maybe invest in books like Programming: Principles and Practice Using C++ 2nd Edition, C++ Primer (not to be confused with C++ Primer Plus), The C++ Standard Library Tutorial and Reference, and The C++ Programming Language (purely as a reference to the language). Above all else, program and practice, always practice.

Now as for your code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <string>
#include <cctype>

using namespace std;

void upper(char []);
void lower(char []);
void non(char []);
void words(char []);

int main()
{
	int choice = 0; // changed to int so your switch works
	char ch = ' ', str[50];
	
	cout<<"*****MENU*****";
	cout<<"\n1.No. of capital alphabets\n2.No. of small alphabets\n3.No. of non-alphabets\n4.No. of Words";
	cout<<"\nenter choice:";
	cin>>choice;
	cin.ignore(1, '\n');
	cout<<"enter a string:";
	cin.getline(str, 50);
	
	switch(choice)
	{
		case 1: upper(str);
			break;
		case 2: lower(str);
			break;
		case 3: non(str);
			break;
		case 4: words(str);
			break;
		default: cout << "wrong choice\n";
			break;
	}
	
	cin >> ch; // just to make it hold, enter a character and press enter to close
	return 0;
}

void upper(char ch[])
{
	int count=0;
	for(int i=0;ch[i]!='\0';i++)
		if(isupper(ch[i]))
			count++;
	cout<<"No. of capital alphabets:"<<count;
}
void lower(char ch[])
{
	int count=0;
	for(int i=0;ch[i]!='\0';i++)
		if(islower(ch[i]))
			count++;
	cout<<"No. of small alphabets:"<<count;
}
void non(char ch[])
{
	int count=0;
	for(int i=0;ch[i]!='\0';i++)
		if(!isalpha(ch[i]))
			count++;
	cout<<"No. of non-alphabets:"<<count;
}
void words(char ch[])
{
	int count=1;
	for(int i=0;ch[i]!='\0';i++)
		if(ch[i]==' ')
			count++;
	cout<<"No. of words:"<<count;
}
I'm guessing Bloodshed's Dev-C++
My guess would be something much older, a DOS program called Turbo C++. Unfortunately educational courses in some parts of the world insist on using this, and much as we can advise against it, the student is often not in a position to change, as the course is dependent on it. (Giveaway signs are the clrscr() function as well as outdated headers).
Oh, I saw the conio header and thought it was Windows, but I never used the header because I was always told to avoid it. Learn something new every day.

New conio was DOS, which is why I thought Dev-C++ (assumed a console program when I saw conio). Didn't know clrscr() was Turbo C++ though.
Last edited on
Topic archived. No new replies allowed.