Breaker??

when i runned my program i run into somekind of error. it compiled program, but when runned i started to getl oads and laods of errors. Can somone help me to fix these errors.
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int long long fibo(int n);
string output_formatted_string(string u);
string x[70];
int main (void) {
int n= 0;
string c = 0;
cout << "Enter a number: ";
cin >> n;
fibo(n);
output_formatted_string(c);
for(int f = 0; f<= 70; f++){
cout << x[f] << endl;
}
system("PAUSE");
return 0;
}

long long fibo(int n)
{
int i = 0;
if (n <2)
return 1;
long long temp1 = 1;
long long temp2= 2;
long long total = 0;
stringstream l;
while (n-- > 1)
{
total = temp1 + temp2;
temp2 = temp1;
temp1 = total;
l << total;
x[i] = l.str();
i++;
}
return 1;
}

#define GROUP_SEP ','
#define GROUP_SIZE 3

string output_formatted_string(string u){
stringstream temp, out;
string num;
for(int d = 0; d <= 70; d++){
	num = x[d];
temp << num;
string s = temp.str();
int n = s.size() % GROUP_SIZE;
int i =0;
if(n > 0 && s.size() > GROUP_SIZE){
out << s.substr(i, n) << GROUP_SEP;
i += n;
}

n = s.size() / GROUP_SIZE -1;
while (n-- > 0){
out << s.substr(i, GROUP_SIZE) << GROUP_SEP;
i += GROUP_SIZE;
}
out << s.substr(i);
x[d] = out.str();
}
return u;
}
closed account (z05DSL3A)
Your for loops on lines 16 and 50 are going out of bounds, should be for(int f = 0; f < 70; f++) and for(int d = 0; d < 70; d++)

Line 11, string c = 0; what are you trying to do here?
out of bound i cna fix by makeing the array bit ibgger. and line 11 i just needed it cause there is no value i want ot osend there i just need to call the function, so taht it would go through my array and formatt every member in there.

i get these 2 errors:
Error 1 error C2664: 'std::basic_string<_Elem,_Traits,_Ax>::basic_string(const std::basic_string<_Elem,_Traits,_Ax> &)' : cannot convert parameter 1 from 'int' to 'const std::basic_string<_Elem,_Traits,_Ax> &' c:\users\...\desktop\c++\39\projekt\projekt\main.cpp 68


2 IntelliSense: no suitable constructor exists to convert from "int" to "std::basic_string<char, std::char_traits<char>, std::allocator<char>>" c:\users\...\desktop\c++\39\projekt\projekt\main.cpp 68
Last edited on
closed account (z05DSL3A)
Just make line 11: string c;
i already got that working ja jsut made it a void. but now, problem is that the errors wont go away. New code is:
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int long long fibo(int n);
string output_formatted_string(void);
string x[71];
int main (void) {
int n= 0;
cout << "Enter a number: ";
cin >> n;
fibo(n);
for(int f = 0; f<= 70; f++){
cout << x[f] << endl;
}
system("PAUSE");
return 0;
}

long long fibo(int n)
{
int i = 0;
if (n <2)
return 1;
long long temp1 = 1;
long long temp2= 2;
long long total = 0;
stringstream l;
while (n-- > 1)
{
total = temp1 + temp2;
temp2 = temp1;
temp1 = total;
l << total;
x[i] = l.str();
i++;
}
output_formatted_string();
}

#define GROUP_SEP ','
#define GROUP_SIZE 3

string output_formatted_string(string u){
stringstream temp, out;
string num;
for(int d = 0; d <= 70; d++){
	num = x[d];
temp << num;
string s = temp.str();
int n = s.size() % GROUP_SIZE;
int i =0;
if(n > 0 && s.size() > GROUP_SIZE){
out << s.substr(i, n) << GROUP_SEP;
i += n;
}

n = s.size() / GROUP_SIZE -1;
while (n-- > 0){
out << s.substr(i, GROUP_SIZE) << GROUP_SEP;
i += GROUP_SIZE;
}
out << s.substr(i);
x[d] = out.str();
}
return 1;
}


but now the problem is in return 1;
Last edited on
closed account (z05DSL3A)
it should be returning a sting not a number.

What is the program meant to be doing?
first 70 fibonacci numbers, writes em into array, then formats the numbers so that the output woud be 43,546,464,546 not 43546464546 and then prints all 70 fibonacci numbers in array.
Last edited on
im getting an error that output_formatted_string must retun a value. but what should i return, i wnat it to go to main and then read out the array.
closed account (z05DSL3A)
im getting an error that output_formatted_string must retun a value.
string output_formatted_string(string u) it need to return a string.


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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <string>

using namespace std;

long long Fib(const int);

string FormatNumber(const long long &num);

int main () 
{
    const int size = 70;
    long long fibSequence[size];
    
    for(int i = 0; i < size; ++i)
    {
        fibSequence[i] = Fib(i);
    }
    
    for(int i = 0; i< size; ++i)
    {
        cout << FormatNumber(fibSequence[i]) << endl;
    }
    
    return 0;
}

long long Fib(const int n)
{
    if ( n == 0 || n == 1 ) 
        return n;
 
    long long fib1 = 0; 
    long long fib2 = 1;
    long long fib = 2;
 
    for ( int i = 2; i <= n; i++ ) 
    {
        fib = fib1 + fib2;
        fib1 = fib2;
        fib2 = fib;
    }

    return fib;
}

string FormatNumber(const long long &num)
{
    const string thousands = ",";
    int i = 0;
    long long tempNum = num;
    string fmt,digit;
    do 
    {
        if((i !=0) && !(i % 3)) 
        {
            fmt = thousands + fmt;
        }
        digit = (tempNum % 10) + '0';
        fmt = digit + fmt;
        tempNum /= 10;
        i++;
    }while((tempNum) || (i < 1));
    
    return fmt;
}
Last edited on
im stuck here
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int long long fibo(int n);
string output_formatted_string(void);
string x[71];

int main (void) {
int n= 0;
cout << "Enter a number: ";
cin >> n;
fibo(n);
for(int f = 0; f<= 70; f++){
cout << x[f] << endl;
}
system("PAUSE");
return 0;
}

long long fibo(int n)
{
int i = 0;
if (n <2)
return 1;
long long temp1 = 1;
long long temp2= 2;
long long total = 0;
stringstream l;
while (n-- > 1)
{
total = temp1 + temp2;
temp2 = temp1;
temp1 = total;
l << total;
x[i] = l.str();
i++;
}
output_formatted_string();
}

#define GROUP_SEP ','
#define GROUP_SIZE 3

string output_formatted_string(void){
stringstream temp, out;
string num;
for(int d = 0; d <= 70; d++){
	num = x[d];
temp << num;
string s = temp.str();
int n = s.size() % GROUP_SIZE;
int i =0;
if(n > 0 && s.size() > GROUP_SIZE){
out << s.substr(i, n) << GROUP_SEP;
i += n;
}

n = s.size() / GROUP_SIZE -1;
while (n-- > 0){
out << s.substr(i, GROUP_SIZE) << GROUP_SEP;
i += GROUP_SIZE;
}
out << s.substr(i);
x[d] = out.str();
}
return ????;
}


i dunno what to return.
closed account (z05DSL3A)
Okay, you don't seem to be using the value returned from output_formatted_string() so why not make it

1
2
3
4
5
6
void output_formatted_string();
void output_formatted_string()
{
   //....
   return;
}


or return out.str();
Last edited on
ok i did it , but now whe n i run it i get this endles amount of numbers cause:

Warning 1 warning C4715: 'fibo' : not all control paths return a value c:\users\...\desktop\c++\39\projekt\projekt\main.cpp 41
closed account (z05DSL3A)
I think you need to read up on functions again.

http://www.cplusplus.com/doc/tutorial/functions/
something has gone wrong
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
long long fibo(int n)
{
int i = 0;
if (n <2)
return 1;
long long temp1 = 1;
long long temp2= 2;
long long total = 0;
stringstream l;
while (n-- > 1)
{
total = temp1 + temp2;
temp2 = temp1;
temp1 = total;
l << total;
x[i] = l.str();
i++;
}
output_formatted_string();
return 1;
}


It doesent calculate fibo

i get:
3
34
348
3481
34811
348111
and so on
Last edited on
i dont have naything to return from fibo. i just need it to go back to main and finsih its probram. cause array has already been filled.
It is almost fully functional only problem is tha fibo does not calculate fibonacci anymore, just gives em random numbers.
closed account (z05DSL3A)
You are doing it a very odd way, however, the problem is the stringstream. In your loop you never clear the contents of it, so you jest keep adding number to it. Move line 9 [stringstream l;] to inside the while loop.
i was told to manipulet the origial code not to write enirely new. i moved stringstream l into while loop, i stlee get endles amountso f numbers.
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
47
48
49
50
51
52
53
54
55
56
57
58
cin >> n;
fibo(n);
for(int f = 0; f<= 70; f++){
cout << x[f] << endl;
}
system("PAUSE");
return 0;
}

long long fibo(int n)
{
int i = 0;
if (n <2)
return 1;
long long temp1 = 1;
long long temp2= 2;
long long total = 0;
while (n-- > 1)
{
stringstream l;
total = temp1 + temp2;
temp2 = temp1;
temp1 = total;
l << total;
x[i] = l.str();
i++;
}
output_formatted_string();
return 1;
}

#define GROUP_SEP ','
#define GROUP_SIZE 3

void output_formatted_string(void){
stringstream temp, out;
string num;
for(int d = 0; d <= 70; d++){
	num = x[d];
temp << num;
string s = temp.str();
int n = s.size() % GROUP_SIZE;
int i =0;
if(n > 0 && s.size() > GROUP_SIZE){
out << s.substr(i, n) << GROUP_SEP;
i += n;
}

n = s.size() / GROUP_SIZE -1;
while (n-- > 0){
out << s.substr(i, GROUP_SIZE) << GROUP_SEP;
i += GROUP_SIZE;
}
out << s.substr(i);
x[d] = out.str();
}
return;
}


if u can try running it yourself
closed account (z05DSL3A)
i was told to manipulet the origial code not to write enirely new.

I believe that your current path is a little off track. If this is homework it might be worth just checking you are on the correct path. If you want post the original code and assignment.

i moved stringstream l into while loop, i stlee get endles amountso f numbers.

In that case you also have a problem in output_formatted_string()

fibo() still dose not calculate the correct number, temp1 should be 0 (zero) and temp2 should be 1 (one).

To satisfy yourself that the looping number problem is in output_formatted_string(), comment out the call to it at the end of fibo() build and run the program.

I don't have the time at the moment to look at output_formatted_string() to work out what you are trying to do but from a quick look it seems that you are not clearing the stringstream and just keep adding number to it.
Last edited on
hat was the problem, in output_formatted_string() i didnt clear hte string stream, that caused the error. now its fixed and working well
Topic archived. No new replies allowed.