Getting double returns when using a function

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 1003 Views - Last Post: 18 December 2016 - 09:53 AM Rate Topic: -----

#1 jv1597   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 143
  • Joined: 18-October 16

Getting double returns when using a function

Posted 16 December 2016 - 04:24 PM

I'm getting double returns when using a simple function with an integer return. The code for the function is as follows:

int pipctr(string lndta)
{
	// OPERATIONAL
	int i = 0;
	string pip = "|";
	size_t ppos = lndta.find(pip);
	while (ppos != string::npos)
	{
		ppos = lndta.find(pip, ppos + 1);
		i++;
	}
	int pips = i;

	return pips;
}



The code in main() is as follows:

dpips = pipctr(lndta) + 1;
cout << dpips << '\n';



The output is:
1
1

Any ideas as to what is causing it to cout double the output?

Is This A Good Question/Topic? 0
  • +

Replies To: Getting double returns when using a function

#2 jimblumberg   User is online

  • member icon

Reputation: 5537
  • View blog
  • Posts: 17,148
  • Joined: 25-December 09

Re: Getting double returns when using a function

Posted 16 December 2016 - 04:31 PM

Nope, not a clue. Perhaps you could post a a small complete program that illustrates the problem instead of a couple of unrelated snippets?


Jim
Was This Post Helpful? 0
  • +
  • -

#3 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Getting double returns when using a function

Posted 16 December 2016 - 04:43 PM

You need to post the rest of your code
Was This Post Helpful? 0
  • +
  • -

#4 jv1597   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 143
  • Joined: 18-October 16

Re: Getting double returns when using a function

Posted 16 December 2016 - 04:47 PM

View Postjimblumberg, on 16 December 2016 - 04:31 PM, said:

Nope, not a clue. Perhaps you could post a a small complete program that illustrates the problem instead of a couple of unrelated snippets?


Jim


Jim, what do you mean by "unrelated snippets"? Here is the code:


pip_header.h code is as follows:

#ifndef PIP_HEADER_H_
#define PIP_HEADER_H_

int pipctr(string lndta)
{
	// OPERATIONAL
	int i = 0;
	string pip = "|";
	size_t ppos = lndta.find(pip);
	while (ppos != string::npos)
	{
		ppos = lndta.find(pip, ppos + 1);
		i++;
	}
	int pips = i;

	return pips;
}

#endif





The main() code is as follows:

#include <iostream>
#include <string>

#include "pip_header.h"

int main()
{
     string lndta = "This is a line | consisting of | pipes for | counting.";
     int dpips = 0;
     int cpos = 0;
     
     dpips = pipctr(lndta);  // cpos is the current position of the pointer, which is irrelevant at this point
     cout << dpips << '\n';  // the function works well, but the output is doubled

     return 0;
}



Any ideas?
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3798
  • View blog
  • Posts: 13,756
  • Joined: 08-August 08

Re: Getting double returns when using a function

Posted 16 December 2016 - 05:11 PM

This code:
#include <iostream>
#include <string>

using namespace std;

int pipctr(string lndta)
{
	// OPERATIONAL
	int i = 0;
	string pip = "|";
	size_t ppos = lndta.find(pip);
	while (ppos != string::npos)
	{
		ppos = lndta.find(pip, ppos + 1);
		i++;
	}
	int pips = i;

	return pips;
}

int main(int argc, const char * argv[]) {

	string lndta = "This is a line | consisting of | pipes for | counting.";
	int dpips = 0;
	int cpos = 0;

	dpips = pipctr(lndta);  // cpos is the current position of the pointer, which is irrelevant at this point
	cout << dpips << '\n';  // the function works well, but the output is doubled

	return 0;
}


produced this output on my system:
3

Changing the function to:
int pipctr(string lndta)
{
	// OPERATIONAL
	int i = 0;
	string pip = "|";
	size_t ppos = lndta.find(pip);
	while (ppos != string::npos)
	{
		ppos = lndta.find(pip, ppos + 1);
		i++;
	}

	return i;
}


also returned 3. No need for pips.

In both cases, int cpos = 0; shows as an unused variable.

This post has been edited by CTphpnwb: 16 December 2016 - 05:12 PM

Was This Post Helpful? 0
  • +
  • -

#6 jv1597   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 143
  • Joined: 18-October 16

Re: Getting double returns when using a function

Posted 16 December 2016 - 05:56 PM

The function works fine, it just returns a double value, such as 33. If the value is 1, or 2, it returns, 11, or 22.
Was This Post Helpful? 0
  • +
  • -

#7 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Getting double returns when using a function

Posted 16 December 2016 - 06:34 PM

My only guess is that your output buffer was not yet marked free by the time the program finished executing, thus resulting in a double flush once the program ended. Maybe try a std::cout.flush() before return 0.

What compiler are you using? what platform?
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6343
  • View blog
  • Posts: 21,765
  • Joined: 05-May 12

Re: Getting double returns when using a function

Posted 16 December 2016 - 06:51 PM

I have serious doubts that the code in post #4 is actually the code being used by the OP. Notice that he uses the string class and the cout object without the namespace specifier, yet he never has the line using namespace std; anywhere in the code presented in that post. How does it even compile?

If that is not the code that he is running, how do we know that there is not an extra cout << dpips << '\n'; or cout << pips << '\n'; floating around in the actual code?
Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3798
  • View blog
  • Posts: 13,756
  • Joined: 08-August 08

Re: Getting double returns when using a function

Posted 16 December 2016 - 07:07 PM

Yes, it's amazing how many people want us to debug through a straw. It's worse when they point the straw in the wrong places!
Was This Post Helpful? 0
  • +
  • -

#10 jv1597   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 143
  • Joined: 18-October 16

Re: Getting double returns when using a function

Posted 16 December 2016 - 07:19 PM

Sorry y'all, I posted some of the code to narrow the scope, to simplify. I thought you would assume that the namespace was understood, and what the main scope was all about. As it turns out, I had the return coded within a loop that was looping once more over the amount it's supposed to loop. So it would return the value twice over.

I'm also using a simple while negation:

while (--dpips)
{

}



The loop expression negates one extra on entry, so it gets to be confusing.
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6343
  • View blog
  • Posts: 21,765
  • Joined: 05-May 12

Re: Getting double returns when using a function

Posted 17 December 2016 - 10:46 AM

Nowhere in post #1 or #4 did you mention that the call to the function and subsequent printing of values was inside a loop. Were readers supposed to be clairvoyant and be able to read code on your screen, or psychic to read your mind and know that a loop was involved?
Was This Post Helpful? 0
  • +
  • -

#12 jv1597   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 143
  • Joined: 18-October 16

Re: Getting double returns when using a function

Posted 17 December 2016 - 04:48 PM

I completely forgot to get that part in. But the issue is resolved anyway, now I'm having problems with the switch. It's a pretty big switch.

It goes from main, to a header, then from a header to the main switch, then from the main switch to a subswitch. So I get case 1 in the main switch (which consists of case1, and case2) to work, but I don't get anything from case 2 for some reason.

Do you know if there is a limit to the amount of code a switch can take?
Was This Post Helpful? 0
  • +
  • -

#13 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Getting double returns when using a function

Posted 17 December 2016 - 05:18 PM

Also, never put function definitions directly into a header. If you are using multiple source files including that header, you will get function redefinition errors. Remember that #include is simply a copy and paste.

You should define function prototypes in header, then use a seperate source for function definitions
Was This Post Helpful? 0
  • +
  • -

#14 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6343
  • View blog
  • Posts: 21,765
  • Joined: 05-May 12

Re: Getting double returns when using a function

Posted 17 December 2016 - 05:20 PM

Headers don't run code. They declare code that gets put into your compilation module which then gets called at runtime. Code execution in C/C++ starts in main().

Anyway, post the minimal code to reproduce your problem. As you saw, bad things happen when you assume people know what you are talking about.

There is no limit to the number of switch statements other than the memory capacity of your OS to load and run your program. Back in the DOS days, where only 64K segments used to be allowed, people found tricks to load in code on demand to get around the limitations. One could theoretically use similar tricks with modern OSes of the 64-bit address space still is not enough.
Was This Post Helpful? 0
  • +
  • -

#15 jv1597   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 143
  • Joined: 18-October 16

Re: Getting double returns when using a function

Posted 17 December 2016 - 06:56 PM

View Postjjl, on 17 December 2016 - 05:18 PM, said:

Also, never put function definitions directly into a header. If you are using multiple source files including that header, you will get function redefinition errors. Remember that #include is simply a copy and paste.

You should define function prototypes in header, then use a seperate source for function definitions


jjl,
Did you mean that I should use header files for prototype declarations, and separate .cpp files for functions?

View PostSkydiver, on 17 December 2016 - 05:20 PM, said:

Headers don't run code. They declare code that gets put into your compilation module which then gets called at runtime. Code execution in C/C++ starts in main().


Should I create one .cpp file and place all of the function within it, and call them from there? Or should the functions be placed within the same .cpp file as the main() one?

They're pretty big functions, and switches.

Found some good information on the following link:

http://www.cplusplus...icles/yAqpX9L8/
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2