HI I was writing a program that passes a string into an array, reverses the string, and passes to another array.
Half way done with my function definition I compiled and got some errors, that I am not able to understand. Here is my code:

// Hw-4_Bhasin.cpp : Defines the entry point for the console application.

void rev_str(void);
double Mean(const int Data[5][4], int, int);
void frequency(const int Data[5][4], int, int);
#include "stdafx.h"
#include<iostream>
#include<string>
#include<cmath>
using namespace std;



int main()
{
	
	char option;

	cout<<"\n	Please choose from the given menu.";
	cout<<"\n	R{Reverse String]	M[Matrix]	Q[Quit]."<<endl;
	cin>> option;

	switch(option)
	{
	case 'R':
		 rev_str();
		 break;
	case 'r':
		rev_str();
		break;

	}
	system("pause");
	return 0;
}

void rev_str(void)
{
	const int MAX = 100;
	char Input_String[MAX];
 
	cout<<"Please enter a string."<<endl;
	cin.get(Input_String, MAX);

	cout<<Input_String;

	for(int i=0; i<MAX; i--)
		cout<<Input_String<<endl;

	system("pause");
	return;
}

The error states that' 'rev_str' identifier not found. In other words it does not tecognize the two function calls in the switch statements rather it is reading it as some sort of variable.

To overcome this I made the function prototypr local. After doing that, the program compiles, but on running the program when I hit r or R, the screen blanks out.

How can I resolve this.
Any help is appreciated.

Thanks.

Recommended Answers

All 6 Replies

Does moving your function prototypes help?

// Hw-4_Bhasin.cpp : Defines the entry point for the console application.


#include "stdafx.h"
#include<iostream>
#include<string>
#include<cmath>

void rev_str(void);
double Mean(const int Data[5][4], int, int);
void frequency(const int Data[5][4], int, int);   

using namespace std;



int main()
{
	
	char option;

	cout<<"\n	Please choose from the given menu.";
	cout<<"\n	R{Reverse String]	M[Matrix]	Q[Quit]."<<endl;
	cin>> option;

	switch(option)
	{
	case 'R':
		 rev_str();
		 break;
	case 'r':
		rev_str();
		break;

	}
	system("pause");
	return 0;
}

void rev_str(void)
{
	const int MAX = 100;
	char Input_String[MAX];
 
	cout<<"Please enter a string."<<endl;
	cin.get(Input_String, MAX);

	cout<<Input_String;

	for(int i=0; i<MAX; i--)
		cout<<Input_String<<endl;

	system("pause");
	return;
}

Like I said on making the function prototype local, the program compiles and runs but it does not execute the function body.
To be more specific, when the user inputs r or R, the switch statement is suppose to call the rev_str() function and then the function body should execute. But when I enter r or R, the screen blanks out(nothing happens.)

This is where the problem is:

for(int i=0; i<MAX; i--)
		cout<<Input_String<<endl;

You initialize variable "i" to zero, decrement it in every step, until it is smaller than MAX (which is 100). So, "i" would go from 0 to -1, -2, -3, -4 until to some large negative number like -2^16. You should initialize "i" to the end of the string, then decrement it to zero.

Also, you are trying to print out the entire string, instead of printing character by character. You should do something like this:

cout<<Input_String[i];

Thanks. It worked fine. But it still gives me an error if I don't declare the function rev_str() as local. The error states that: rev_str: identifier not declared.
In other words it is not recognizing the function call in the switch statements, if I do not declare the function as local function.

hmm not to sure why that would be it seems ok to me....

one thing i wanna point out is as follow's its just a little neater

case 'R':
                rev_str();
	break;
case 'r':
	rev_str();
	break;

is the same as

case 'R':
case 'r':
	rev_str();
	break;

Chris

Thanks 4 the suggestion.

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.