Outputting C++ random walk to Excel

Hi everybody I've been suffering from this very annoying issue I was hoping someone might be able to help me resolve here. I've created a program that generates a 1 dimensional random walk for a "particle" starting at a position zero, with a step size and a bound for the random walk which the user can decide via their input (hence the particle "falls off the ledge" when reaching the bound, e.g. of size 500 if it goes beyond 500 or -500).

I've been able to get a series that spits out the data tracking the location of the particle with each iteration printed on the terminal using the cout command. However, when I tried to make it output to an Excel CSV I got an error saying

"error: invalid operands of type 'int'and '<unresolved overloaded function type>' to binary 'operator<<'"

Here is the code I've used:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<iostream>
#include <stdio.h>
#include <cstdlib>
#include <time.h>
#include <fstream>
using namespace std;
int main()
{
	cout<<"\nWelcome to the random walk!"<<endl;
	int a;
	int b;
	int t;
	t=time(NULL);
	int t0;
	int tm;
	t0=t;
	srand(time(NULL));
	cout<<"The time today is: ";
	cout<<t<<endl;
	cout<<"Yo dawg! Give me the step witdth for the random walk:  "<<endl;
	cin>>a;
	cout<<"Dude! What's the width of the plank?  "<<endl;
	cin>>b;
	int s;
	int r;
	s=0;
	ofstream MyExcelFile;
	MyExcelFile.open("example.csv");
	while (s*s<b*b)
	{	
		tm=t-t0;
		cout<< s << " | " << tm << endl;
		MyExcelFile << s,tm << endl;
		r=(rand()%10)+1;
		if (r>5)
		{
  			s=s+a;
		}
		else 
		{
	  		s=s-a;
		}
		t++; 
	}
	MyExcelFile.close();
}

Could someone help me identify the problem here, and what I need to change to correct this?

Thanks so much in advance!
Last edited on
- Use [code][/code] tags
- Show us the line of code that is causing the compiler error
That error means something is wrong with the way you're using <<

Looking, we see MyExcelFile << s,tm << endl;, which needs to be:
MyExcelFile << s << ',' << tm << endl;

I thought csv didn't have the '|' delimiter.
Last edited on
Hi guys, thanks so much for the help. It turns out putting double inverted commas round the comma in the line where the output is to MyExcelFile solved the problem.

It therefore now reads:
MyExcelFile << s << "," << tm << endl;

and now it works, so thanks a lot! ;)
Topic archived. No new replies allowed.