scanf() acting more than weird

When compiled, code returns no errors. (it is strange, it returns no errors even if you dont include cstdio header...)

well, here's the code:

#include<cstdio>
#include<vector>
using namespace std;
...
scanf("%d",&maxcount);
for(int i=0; i<maxcount; ++i)
{
scanf("%d",&value);
storage.push_back(value);
}
...

When I enter some integer K, it loops only K times, but after last scanf() the programs just ends, eventhough I got more code afterwards.
I wrote a simple printf("FOR LOOP SUCCESSFUL"); after the for loop, but it never gets printed.
Anyone has a suggestion?

Another issue...
using the bitset function to_string().
I won't compile complaining that there is no matching function to call.
[820 byte] By [XZminX] at [2007-11-19 18:30:25]
# 1 Re: scanf() acting more than weird
Please post compilable code that you had problems with. I could not compile due to the "...". Regards.
exterminator at 2007-11-9 0:55:46 >
# 2 Re: scanf() acting more than weird
There is no apparent error in the code you posted.
I guess that the error is in the code following the for loop.

I wrote a simple printf("FOR LOOP SUCCESSFUL"); after the for loop, but it never gets printed.

The fact that it stops without requesting for more input make me think that the for loop has been exited correctly.
But, printf uses a buffered output, and I guess that the program crashes before the buffer is flushed.
You should use fflush(stdout); or fprintf to stderr (which should not be buffered).

(it is strange, it returns no errors even if you dont include cstdio header...)

I guess you included another header which internally includes cstdio.
It is not an error for a standard header to expose the content of other headers, although you must not assume that it does.
SuperKoko at 2007-11-9 0:56:57 >
# 3 Re: scanf() acting more than weird
It is a bit bigger code. It also includes set. But program exits before it gets to use set.
The for loop isnt exited correctly. I also put printf() after the scanf() within the for loop and it doesnt get printed when scanf() is called for the last time.

#include<cstdio>
#include<vector>
using namespace std;
vector<int> storage;
int main(void)
{
storage.reserve(20);
int maxcount; //never greater than 20
scanf("%d",&maxcount);
for(int i=0; i<maxcount; ++i)
{
int value;
scanf("%d",&value);
storage.push_back(value);
}
printf("INPUT WAS SUCCESSFUL\n");
return 0;
}
I have tried runing this code only and it works. Wonder why. Perhaps only other things being there makes the code unstable. I'll have to into it tomorrow. I should prepare for maths tomorrow. (City contest, jeee)
XZminX at 2007-11-9 0:57:48 >
# 4 Re: scanf() acting more than weird
Use a debugger, if you have one.

The for loop isnt exited correctly. I also put printf() after the scanf() within the for loop and it doesnt get printed when scanf() is called for the last time.

Are you sure to call fflush(stdout); after each call to printf?
SuperKoko at 2007-11-9 0:58:50 >
# 5 Re: scanf() acting more than weird
I also tried cout << "something" << endl; which flushes the buffer

The problem: The task was to write a program that takes N and then N integers (which I store in vector). My task is to calculate all possible sums based on those integers. As you can see below, I use brute force (with recursion) method to get the sums and then I store sums in the set (to eliminate duplicates). Easy thing, but I don't know what went wrong...
Here's the complete code:
#include<cstdio>
#include<vector>
#include<set>
using namespace std;
vector<int> storage;
set<int> combinations;
int sum;
int total_combinations;
int max_coins;

void make_combinations(int coins)
{
if(coins==max_coins)
{
if(combinations.insert(sum).second==true) ++total_combinations;
return;
}
for(unsigned int v=0; v!=storage.size(); ++v)
{
sum+=storage.at(v);
make_combinations(++coins);
sum-=storage.at(v);
}
return;
}

int main(void)
{
storage.reserve(20);
int maxcount; //never greater than 20
scanf("%d\n",&maxcount);
for(int i=0; i<maxcount; ++i)
{
int value;
scanf("%d",&value);
storage.push_back(value);
}
printf("INPUT OK\n");
for( int j=1; j<=maxcount; ++j)
{
sum=0;
max_coins=j+1;
make_combinations(1);
}
printf("%d\n",total_combinations);
return 0;
}
XZminX at 2007-11-9 0:59:53 >
# 6 Re: scanf() acting more than weird
Use a debugger to view the maxcount variable after your first scanf(). Also, check the return value of scanf() (it should return 1 for 1 successful conversion, not 0) to make sure the conversion worked. The performance of scanf() might depend upon your OS's line-ending character sequence. I'm not sure that you can use them in the format string, and if you're running on Windows, the line-ending sequence is actually "\r\n", so the conversion would fail. This would result in maxcount potentially containing garbage, and your for loop would then run a possibly really large number of times.
Bob Davis at 2007-11-9 1:00:54 >
# 7 Re: scanf() acting more than weird
scanf is not a problem with your code.
But, you have an infinite recursion, which loops until the stack overflows (Windows displays a pretty, very informative, message box).

Your program has two bugs:
1st: The recursive call make_combinations(++coins) should be make_combinations(coins+1).

If there are more than one integer, with ++coins, it will make coins greater than max_coins (which is 1 at the start), at the second iteration of the for loop inside make_combinations.
Then, there will be an infinite recursion without stopping condition, because coins>max_coins.

2nd:
You allow sums done with maxcount+1 integers (because of the <= in the for loop of the main function.

Here is a more correct code:

#include<cstdio>
#include<vector>
#include<set>
using namespace std;
vector<int> storage;
set<int> combinations;
int sum;
int total_combinations;
int max_coins;

void make_combinations(int coins)
{
if(coins==max_coins)
{
if(combinations.insert(sum).second==true) ++total_combinations;
return;
}
else
{
for(unsigned int v=0; v!=storage.size(); ++v)
{
sum+=storage.at(v);
make_combinations(coins+1);
sum-=storage.at(v);
}
}
return;
}

int main(void)
{
int maxcount; //never greater than 20
scanf("%d\n",&maxcount);
storage.reserve(maxcount);
for(int i=0; i<maxcount; ++i)
{
int value;
scanf("%d",&value);
storage.push_back(value);
}
printf("INPUT OK\n");
for( int j=1; j<maxcount; ++j)
{
sum=0;
max_coins=j+1;
make_combinations(1);
}
printf("%d\n",total_combinations);
return 0;
}

Also, I don't like much the idea of making the "coins" variable begin with the value 1.
You need to make max_coins=j+1, which is not very intuitive and perhaps leaded to the error of the "<=" comparison.

So, it could be better to use that:

max_coins=j;
make_combinations(0);

PS: I am not sure what you mean by "all sums", but currently, for a set such as {1 ,7,5}, it includes 1 , 1+1, and 1+1+1 and many sums where a number appear twice or three times.
If you don't want to include them, then you should use a correct algorithm, knowing that it is as simple as counting in binary format (1 meaning that the number is included in the sum, and zero meaning that the number is not included in the sum), I let you find the solution : It is easy with a recursion.
SuperKoko at 2007-11-9 1:01:53 >
# 8 Re: scanf() acting more than weird
The algorithms is ok. I need to include these sums also.

I'll be ****!!! the problem was, as you suspected, in the passing of coins value in recursive function. Thanks a lot SuperKoko.

The \n in my first scanf() was just a habit. It really makes no difference. (Which is not the case when gets() or fgets() follows scanf())
XZminX at 2007-11-9 1:02:59 >
# 9 Re: scanf() acting more than weird
...
Another issue...
using the bitset function to_string().
I won't compile complaining that there is no matching function to call.

see ...

http://www.dev-archive.com/forum/showthread.php?t=369956

It seems like different compilers require different syntax.
Philip Nicoletti at 2007-11-9 1:04:02 >
# 10 Re: scanf() acting more than weird
As you obviously writing C++ and not C, why do you use scanf in the fist place instead of cin >> maxvalue;:confused:
treuss at 2007-11-9 1:05:03 >
# 11 Re: scanf() acting more than weird
I use both. When I know the the type of input certainly and need every ms of CPU time, I use scanf(), when not so, or when I don't wanna bother with all that writing :).

@Philip: Thanks a lot for the link. Just what I needed.
XZminX at 2007-11-9 1:06:01 >