0

It is a big program. I stripped off unnecessary code. I left only one of the key functions

When I call ss(); in any function the function gives control back to main() without accepting a string. The code works if I don't use a function to accept the string. I can't find anything wrong with it.

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<ctype.h>
void ss();
void casechange();
using namespace std;
char str[100];
int main (){
int choice;

cout<<"Make a choice"<<endl; 
cout<<"Press 1 to change the case of alphabets"<<endl;
cout<<"Press 2 to count number of vowels"<<endl;
cout<<"Press 3 to check if entered string is a palindrome or not"<<endl;
cout<<"Press 4 to reverse a string"<<endl;
cout<<"Press 5 to count number of words"<<endl;
cin>>choice;
switch(choice){
case 1: casechange();
    break; 
case 2: vowelcount();
        break;
case 3:pal();
    break;
case 4: rev();
    break;
case 5: wordcount();
    break;
default: cout<<"Wrong choice"<<endl;
 }
 return 0;
}
void casechange(){
ss();
for(int i=0;str[i]!='\0';i++)
 {
 if(isupper(str[i]))
 str[i]=tolower(str[i]);
 else str[i]=toupper(str[i]);
 }
 puts(str);
}
void ss()
{
cout<<"Enter a string"<<endl;
 gets(str);
}

p.s. I am using code blocks. gcc compiler I guess.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 3
    (1) You're mixing C standard I/O with C++ standard streams. It works, but it is bad style. (2) You're using C `gets()`; that would be an instant failure were I in charge of your code. It is a lethal function that cannot be used safely in any code. You may get away with it for a while, but not for ever. (3) Your problem is almost certainly that the first input leaves a newline in the input buffer, and the `gets()` call in `ss()` reads that newline and finishes. There are a lot of questions with the same basic problem. – Jonathan Leffler Oct 01 '15 at 19:50
  • 1
    You are currently mixing C++-style input (cin) and C-style input (gets). I'd wager that's the issue here. My recommendation would be to switch to just using C++-style input. Then, switch from using raw char arrays to using `std::string`, which is easier to use and less error-prone. – templatetypedef Oct 01 '15 at 19:51
  • Also, please study how to create an MCVE ([How to create a Minimal, Complete, and Verifiable Example?](http://stackoverflow.com/help/mcve)). You've done a fairly good job, but you could eliminate the options other than `casechange()` to make it (a) more minimal and (b) self-contained so it can be compiled and tested. (See also: SSCCE ([Short, Self-Contained, Correct Example](http://sscce.org/)) — a similar exposition to the MCVE one.) – Jonathan Leffler Oct 01 '15 at 19:54
  • @JonathanLeffler Thanks I am new here. I will keep that in mind. – yash grover Oct 02 '15 at 02:18

1 Answers1

0

You asked user to make a choice. User typed a number and enter. You then read a single character. enter still sitting there in the buffer. When it comes to gets, it reads it as an empty string.

Also please pay attention to all the comments about IO, gets etc.

user58697
  • 7,808
  • 1
  • 14
  • 28