5 Replies - 1485 Views - Last Post: 17 April 2011 - 05:57 AM Rate Topic: -----

#1 CodeWomanSRQ1234  Icon User is offline

  • New D.I.C Head

Reputation: 17
  • View blog
  • Posts: 34
  • Joined: 08-August 09

C-Strings - have bracket error messages I cannot get rid of. Weird!

Posted 14 April 2011 - 06:09 AM

I am trying to do:

Backward String
Write a function that accepts a C-string as an argument and displays its contents backwards. For instance, if the string argument is “Gravity” the function should display “ytivarG”. Demonstrate the function in a program that asks the user to input a string and then passes it to the function.

Here are the error messages I cannot get rid of:
1>------ Build started: Project: DayOfYear, Configuration: Debug Win32 ------
1> DayOfYear.cpp
1>c:\users\kim\documents\visual studio 2010\projects\dayofyear\dayofyear\dayofyear.cpp(125): fatal error C1075: end of file found before the left brace '{' at 'c:\users\kim\documents\visual studio 2010\projects\dayofyear\dayofyear\dayofyear.cpp(29)' was matched
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Here is my code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstring>

// Not sure if I need all libraries but it can't hurt. :)/>

using namespace std;

int main()
{
	
	// declare string variable to accept user input
	string word;

	// ask for user to type a word
	cout << "Please enter a word and the magical computer will print it backwards: " << word << endl;


	// C-String handling function
	void displayString(char str[] )
	{
		// define variable to use as index
		int index = 0;

		// step through string until null character is reached
		while ( str[index] != '\0' )
		{
			cout << str[index];
			index++;

			declare variable to count the letters
			//int letterCount;

			//  measure length of string entered
			// set idex of memory locations of letters in typed string equal to letterCount
			while ( letterCount != 0; letterCount = index; letterCount--; )
			{
				// create a string to print out each letter as we loop through string length measurement
				string letter;

				// print the letter
				cout << letter << endl;
			} // end while 

		} // end step through

		// end lthe line
		cout << endl;

	} // end C-String handling function
	
	system("Pause");
	return 0;
}


Sorry, posted wrong error messages. Here are the correct ones:

1>------ Build started: Project: BackwardString, Configuration: Debug Win32 ------
1> Backward String.cpp
1>c:\users\kim\documents\visual studio 2010\projects\backwardstring\backwardstring\backward string.cpp(30): error C2601: 'displayString' : local function definitions are illegal
1> c:\users\kim\documents\visual studio 2010\projects\backwardstring\backwardstring\backward string.cpp(19): this line contains a '{' which has not yet been matched
1>c:\users\kim\documents\visual studio 2010\projects\backwardstring\backwardstring\backward string.cpp(45): error C2143: syntax error : missing ')' before ';'
1>c:\users\kim\documents\visual studio 2010\projects\backwardstring\backwardstring\backward string.cpp(45): error C2059: syntax error : ')'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Is This A Good Question/Topic? 0
  • +

Replies To: C-Strings - have bracket error messages I cannot get rid of. Weird!

#2 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 614
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: C-Strings - have bracket error messages I cannot get rid of. Weird!

Posted 14 April 2011 - 06:25 AM

In C and C++, function definitions cannot be embedded in other functions, they are stand alone modules. Have a read of JackOfAllTrades tutorial on functions.
Was This Post Helpful? 1
  • +
  • -

#3 muballitmitte  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 174
  • View blog
  • Posts: 470
  • Joined: 05-November 08

Re: C-Strings - have bracket error messages I cannot get rid of. Weird!

Posted 14 April 2011 - 06:25 AM

move your displayString implmentation above main(). What were you exactly trying to do here

 while ( letterCount != 0; letterCount = index; letterCount--; )




You can read this tutorial http://www.dreaminco...ing-loops-in-c/ about loops in any case
Was This Post Helpful? 1
  • +
  • -

#4 CodeWomanSRQ1234  Icon User is offline

  • New D.I.C Head

Reputation: 17
  • View blog
  • Posts: 34
  • Joined: 08-August 09

Re: C-Strings - have bracket error messages I cannot get rid of. Weird!

Posted 14 April 2011 - 10:34 AM

View PostAphex19, on 14 April 2011 - 07:25 AM, said:

In C and C++, function definitions cannot be embedded in other functions, they are stand alone modules. Have a read of JackOfAllTrades tutorial on functions.


Can u tell me which part of the code are you referring to? i got the C-string function from my class.
Was This Post Helpful? 0
  • +
  • -

#5 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 614
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: C-Strings - have bracket error messages I cannot get rid of. Weird!

Posted 14 April 2011 - 10:45 AM

View PostCodeWomanSRQ1234, on 14 April 2011 - 11:34 AM, said:

View PostAphex19, on 14 April 2011 - 07:25 AM, said:

In C and C++, function definitions cannot be embedded in other functions, they are stand alone modules. Have a read of JackOfAllTrades tutorial on functions.


Can u tell me which part of the code are you referring to? i got the C-string function from my class.


You "displayString" function is defined within another function (the main), when it should be defined outside of any function. Here's an example.

Mistake you're making
int main()
{
    void displayString()
    {
    }
}



How it should be.
void displayString()
{
}

int main()
{

}


Functions cannot be defined inside of other functions.
Was This Post Helpful? 1
  • +
  • -

#6 CodeWomanSRQ1234  Icon User is offline

  • New D.I.C Head

Reputation: 17
  • View blog
  • Posts: 34
  • Joined: 08-August 09

Re: C-Strings - have bracket error messages I cannot get rid of. Weird!

Posted 17 April 2011 - 05:57 AM

I actually redid this program. Here is the updated code
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstring>

// Not sure if I need all libraries but it can't hurt. :)/>

using namespace std;


// C-String handling function
void Backward (char *);

int main()
{
// define array for letters in a word entered		
char word[41];

// ask user for input
cout << "This program will display the letters of a word entered in backwards order"
	"follwed by a period." << endl;
cout << "Please enter a word not over 40 letters:" << endl;

// display word entered backwards
cin >> word;
 
cout << "The entered word displayed in reverse is: " << endl;

Backward(word);

cout << endl;
}


void Backward (char *sentencePtr)

{

	char *p = sentencePtr;
	while ( *p != '\0' )

		++p;
 
	while ( p != sentencePtr )

		cout.put ( *--p );
	
	system("Pause");
	return 0;
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1