Program skips functions in main

This is a discussion on Program skips functions in main within the C++ Programming forums, part of the General Programming Boards category; OK, i'm having a few problems with my program below. I want to write a reverse function and call it ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    14

    Program skips functions in main

    OK, i'm having a few problems with my program below. I want to write a reverse function and call it in main [(this is in addition to other functions i have added to mystring class) but any code i've written doesn't work properly. Using the code below it allows me to enter a string and prints it out in reverse. While this isnt an ideal solution it kind of works in that it does reverse the string. Another problem is that after reversing the string, it skips the rest of my main function ie., i want to enter another string and copy it 'n' times. if anyone can see how I can get the rest of program to be called and also how i could call reverrse as a function rather than including it in main I would appreciate any help. Thanks.

    Code:
    // Overloading the function print()
    // and the operator + . In this program,
    // operator+() is a friend function. This
    // time, member functions of the class are
    // defined after the class (i.e.not inline)
    #include <iostream>
    #include <string.h>
    #include <stdlib.h>
    using namespace std;
    const int max_len = 300;
    
    // Definition of the mystring class
    class mystring
    {
    public:
    	
    	void assign(char* t);
    	int length();
    	void print();
    	void print(ostream& os);
    	
    	friend mystring& 
    		operator*(const mystring& a, int n);
    	friend mystring 
    		operator+(const mystring& a, const mystring& b);
    	friend int 
    		operator==(const mystring &str, const mystring &str2);
    		char& operator[](int index);
    	friend istream& operator>>(istream& is, mystring& s);
    	friend ostream& operator<<(ostream& os, mystring& s);
    
    private:
    	char s[max_len];
    	int len;
    };
    // Member functions of the mystring class
    
    
    void mystring::assign(char* t)
    {
    strcpy(s, t);
    len = strlen(s);
    }
    int mystring::length()
    {
    return (len);
    }
    void mystring::print()
    {
    cout << s << "\nLength: " << len << "\n";
    }
    // Prototype of a general function (not a
    // member of the mystring class)
    void print(char* str);
    // The program’s main function
    int main()
    {
      char str[300]; 
      char *start, *end; 
      int len; 
      char t; 
     
      cout << "Original: "; 
      cin >> str;
       
      len = strlen(str); 
     
      start = str; 
      end = &str[len-1]; 
     
      while(start < end) { 
        // swap chars 
        t = *start; 
        *start = *end; 
        *end = t; 
     
        // advance pointers 
        start++; 
        end--; 
      } 
     
     
      cout << "Reversed: " << str << "\n";
    
      {
    	int multiple;
    	mystring testString;
    	mystring copyString;
    
    	
    	cout << "Enter string to be copied: ";
    	cin >> testString;	
    	cout << "Enter number of times to copy: ";
    	cin >> multiple;
    
    	copyString = testString * multiple;
    	cout << copyString << endl;
      }
    }
    // This is the function definition
    // of the overloaded * operator.
    mystring& operator*(const mystring& a, int n)
    {
    	if (a.len * n < max_len)
    	{
    	mystring tmp;
    	strcpy(tmp.s, a.s);
    	for (int  i = 1; i  < n; ++i)
    	{
    		strcat(tmp.s, a.s);
    	}
    	return tmp;
    	}
    	else {
    		cerr << "\nERROR: Max length exceeded\n\n";
    		exit(1);
    }
    }
    
    
    // This is the function definition
    // of the overloaded + operator.
    mystring operator+(const mystring& a, const mystring& b)
    {
    mystring tmp;
    if (a.len + b.len < max_len)
    {
    strcpy(tmp.s, a.s);
    strcat(tmp.s, b.s);
    tmp.len = a.len + b.len;
    }
    else
    
    return tmp;
    }
    
    
    // Definition of the general print function
    void print(char* s)
    {
    	cout << s << "\nLength: " << strlen(s) << "\n";
    }
    
    inline int operator==(const mystring &str,
    const mystring &str2)
    {
    // recall the strcmp function returns
    // 0 if strings match, but this
    // function is to return non-zero
    // (i.e. true) for a match
    return(strcmp(str.s, str2.s) == 0);
    }
    char& mystring::operator[](int index)
    {
    if (index > len-1)
    return s[len-1];
    else
    return s[index];
    }
    istream& operator>>(istream& is, mystring& str)
    {
    const int bufsize = 256;
    char buf[bufsize];
    if (is.get(buf, bufsize))
    str.assign(buf);
    return is;
    }
    ostream& operator<<(ostream& os, mystring& s)
    {
    s.print(os);
    return os;
    }
    
    // general print function for the string
    // class (for any output stream, not just
    // cout as before)
    void mystring::print(ostream& os)
    {
    os << s;
    }
    Last edited by En-Motion; 02-18-2009 at 06:05 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    One: You should probably notice the warnings:
    Code:
    H:\temp.cpp: In function `mystring& operator*(const mystring&, int)':
    H:\temp.cpp:106: warning: reference to local variable `tmp' returned
    H:\temp.cpp: In function `mystring operator+(const mystring&, const mystring&)':
    H:\temp.cpp:135: warning: control reaches end of non-void function
    Two: the code clearly runs, as the prompts are printed. I'm guessing your operator>> doesn't do quite what you think it does.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    14
    Cheers for your help. I'vr changed my code and it's working much better now. I've also added a new overloaded print function where print(int k) will print 1st k digits of a string. When I run the program and type a digit for k when prompted I get a load of symbols instead of charactrs from my string. This is my output when I run the program:

    Test String
    Enter number of times to copy: 2
    Test StringTest String

    String Reversed:
    gnirtS tseT

    Enter k digits to be copied: 2
    ╠╠
    Press any key to continue . . .


    I'm sure it's something simple but at 3 am in the morning I just can't see it.
    Code is below.

    Code:
    // Overloading the function print()
    // and the operator + . In this program,
    // operator+() is a friend function. This
    // time, member functions of the class are
    // defined after the class (i.e.not inline)
    #include <iostream>
    #include <string.h>
    #include <stdlib.h>
    
    using namespace std;
    const int max_len = 300;
    
    // Definition of the mystring class
    class mystring
    {
    public:
    	void reverse(char* s);
    	void assign(char* t);
    	int length();
    	void print(int k);
    	void print();
    	void print(ostream& os);
    	
    	friend mystring& 
    		operator*(const mystring& a, int n);
    	friend mystring 
    		operator+(const mystring& a, const mystring& b);
    	friend int 
    		operator==(const mystring &str, const mystring &str2);
    		char& operator[](int index);
    	friend istream& operator>>(istream& is, mystring& s);
    	friend ostream& operator<<(ostream& os, mystring& s);
    
    private:
    	char s[max_len];
    	int len;
    };
    // Member functions of the mystring class
    void mystring::reverse(char* s)
    {
    	  if(*s != '\0')
             reverse(s+1);	  
    		cout<<*(s);
    }
    
    
    void mystring::assign(char* t)
    {
    strcpy(s, t);
    len = strlen(s);
    }
    int mystring::length()
    {
    return (len);
    }
    
    void mystring::print(int k)
    {
    	char s[300];
    	int i;
    	for (i = 0; i < k; i++)
    	cout << s[i];
    }
    void mystring::print()
    {
    cout << s << "\nLength: " << len << "\n";
    }
    // Prototype of a general function (not a
    // member of the mystring class)
    void print(char* str);
    // The programís main function
    int main()
    {
    	int multiple;
    	int k=0;
    	
    	
    	mystring copyString;
    	mystring one;
    	char two [] = "Test String";
    
    	one.assign(two);
    	cout << one << endl;
    
    	cout << "Enter number of times to copy: ";
    	cin >> multiple;
    
    	copyString = one * multiple;
    	cout << copyString << endl;
    
    	cout << "\nString Reversed: \n";
    	one.reverse(two);
    	cout << endl;
    
    	cout << "\nEnter k digits to be copied: ";
    	cin >> k;
    	one.print(k);
    	cout << endl;
    	 
      
    }
    // This is the function definition
    // of the overloaded * operator.
    mystring& operator*(const mystring& a, int n)
    {
    	if (a.len * n < max_len)
    	{
    	mystring tmp;
    	strcpy(tmp.s, a.s);
    	for (int  i = 1; i  < n; ++i)
    	{
    		strcat(tmp.s, a.s);
    	}
    	return tmp;
    	}
    	else {
    		cerr << "\nERROR: Max length exceeded\n\n";
    		exit(1);
    }
    }
    
    
    // This is the function definition
    // of the overloaded + operator.
    mystring operator+(const mystring& a, const mystring& b)
    {
    mystring tmp;
    if (a.len + b.len < max_len)
    {
    strcpy(tmp.s, a.s);
    strcat(tmp.s, b.s);
    tmp.len = a.len + b.len;
    }
    else
    
    return tmp;
    }
    
    
    // Definition of the general print function
    void print(char* s)
    {
    	cout << s << "\nLength: " << strlen(s) << "\n";
    }
    
    inline int operator==(const mystring &str,
    const mystring &str2)
    {
    // recall the strcmp function returns
    // 0 if strings match, but this
    // function is to return non-zero
    // (i.e. true) for a match
    return(strcmp(str.s, str2.s) == 0);
    }
    char& mystring::operator[](int index)
    {
    if (index > len-1)
    return s[len-1];
    else
    return s[index];
    }
    istream& operator>>(istream& is, mystring& str)
    {
    const int bufsize = 256;
    char buf[bufsize];
    if (is.get(buf, bufsize))
    str.assign(buf);
    return is;
    }
    ostream& operator<<(ostream& os, mystring& s)
    {
    s.print(os);
    return os;
    }
    
    // general print function for the string
    // class (for any output stream, not just
    // cout as before)
    void mystring::print(ostream& os)
    {
    os << s;
    }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Your print-so-many function is printing members of the local char-array s, which has nothing whatsoever to do with your private data char-array s.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    14
    Cheers mate. Works now. I'd like to be able to prompt user for input rather than using "Test String". I can't seem to figure it out though. Do you know where I could look that would point me in the right direction?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I'm guessing that the problem you had with >> came from the fact that >> tends to leave \n in the stream -- you originally read a number, which left \n on the input stream; get came along, saw \n, and grabbed nothing, and even worse left the \n on the stream for later too. Either make reading your "mystring" the first operation, or make sure there's nothing left hanging around to get in the way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program skips fegts function - HELP
    By Tripod87 in forum C Programming
    Replies: 3
    Last Post: 07-22-2008, 09:26 AM
  2. Program with Shapes using Virtual Functions
    By goron350 in forum C++ Programming
    Replies: 12
    Last Post: 07-17-2005, 02:42 PM
  3. Couple of Q's.....
    By oobootsy1 in forum C++ Programming
    Replies: 18
    Last Post: 02-23-2004, 02:03 PM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. Replies: 4
    Last Post: 11-23-2003, 07:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21