#Raindrops C++

118 messages · Page 1 of 1 (latest)

sturdy cedar
#

I'm getting errors in Raindrops.cpp

#

Here's my code

unreal rampart
#

Did you define the function in the header file?

#

Please use codeblocks and not screenshots

sturdy cedar
#

I mean idk how should i code in .h file?

unreal rampart
#

string rain(int RAINDROPS_H);

sturdy cedar
#
#if !defined(RAINDROPS_H)
#define RAINDROPS_H
#include <string>

namespace raindrops {
    string rain(int RAINDROPS_H);
}  // namespace raindrops

#endif // RAINDROPS_H```
#

Error

/tmp/raindrops/raindrops.h:6:5: error: 'string' does not name a type; did you mean 'stdin'?
    6 |     string rain(int RAINDROPS_H);
      |     ^~~~~~
      |     stdin```
#

Okay removed that error using namespace std; in .h file

#

but still have errors

unreal rampart
#

The raindrops namespace needs to contain a convert function

#

It sounds like you didn't define that function inside that namespace in the header?

sturdy cedar
#

No I didnt

#

So i should code the convert function just like cpp in .h file?

unreal rampart
#

You need to define a convert function in the raindrops namespace in the header file

#

You should not include the function code/body. Only the definition/signature/first line.

sturdy cedar
#

Oh okay I'll try

unreal rampart
#

Got it working?

sturdy cedar
#

Nah

#

Idk where Im going wrong

#

raindrops.h

#if !defined(RAINDROPS_H)
#define RAINDROPS_H
#include <string>
using namespace std;

namespace raindrops {
    string rain(int RAINDROPS_H);
    string convert(int RAINDROPS_H);
}  // namespace raindrops

#endif // RAINDROPS_H```
#
#include "raindrops.h"
#include <string>
using namespace std;

namespace raindrops {
    string rain(int RAINDROPS_H){
        string result = "";
        if(RAINDROPS_H % 3 == 0){
            result+= "Pling";
        }
        if(RAINDROPS_H % 5 == 0){
            result+= "Plang";
        }
        if(RAINDROPS_H % 7 == 0){
            result+= "Plong";
        }
        else{
            string convert(int RAINDROPS_H){
                result+= to_string(RAINDROPS_H);
                return result;
            } 
        }
        return result;
    }

    
}  // namespace raindrops
#
/tmp/raindrops/raindrops.cpp:8:24: error: expected primary-expression before '%' token
    8 |         if(RAINDROPS_H % 3 == 0){
      |                        ^
/tmp/raindrops/raindrops.cpp:11:24: error: expected primary-expression before '%' token
   11 |         if(RAINDROPS_H % 5 == 0){
      |                        ^
/tmp/raindrops/raindrops.cpp:14:24: error: expected primary-expression before '%' token
   14 |         if(RAINDROPS_H % 7 == 0){
      |                        ^
/tmp/raindrops/raindrops.cpp:18:44: error: a function-definition is not allowed here before '{' token
   18 |             string convert(int RAINDROPS_H){
      |                                            ^
make[2]: *** [CMakeFiles/raindrops.dir/build.make:90: CMakeFiles/raindrops.dir/raindrops.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:85: CMakeFiles/raindrops.dir/all] Error 2
make: *** [Makefile:91: all] Error 2```
unreal rampart
#

Try replacing RAINDROPS_H with a regular variable name

#

That's a macro

#

(in the function)

sturdy cedar
#

I did now the last error remains

#

18

#



/tmp/raindrops/raindrops.cpp:18:44: error: a function-definition is not allowed here before '{' token
   18 |             string convert(int RAINDROPS_H){
      |                                            ^
make[2]: *** [CMakeFiles/raindrops.dir/build.make:90: CMakeFiles/raindrops.dir/raindrops.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:85: CMakeFiles/raindrops.dir/all] Error 2
make: *** [Makefile:91: all] Error 2```
unreal rampart
#

Try replacing RAINDROPS_H with a regular variable name

sturdy cedar
#

I did

#

same error still

unreal rampart
#

Line 18 still has RAINDROPS_H according to that error message

sturdy cedar
#
/tmp/raindrops/raindrops.cpp:18:42: error: a function-definition is not allowed here before '{' token
   18 |             string convert(int raindrops){
      |                                          ^
make[2]: *** [CMakeFiles/raindrops.dir/build.make:90: CMakeFiles/raindrops.dir/raindrops.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:85: CMakeFiles/raindrops.dir/all] Error 2
make: *** [Makefile:91: all] Error 2```
#
#include "raindrops.h"
#include <string>
using namespace std;

namespace raindrops {
    string rain(int raindrops){
        string result = "";
        if(raindrops % 3 == 0){
            result+= "Pling";
        }
        if(raindrops % 5 == 0){
            result+= "Plang";
        }
        if(raindrops % 7 == 0){
            result+= "Plong";
        }
        else{
            string convert(int raindrops){
                result+= to_string(raindrops);
                return result;
            } 
        }
        return result;
    }

    
}  // namespace raindrops
unreal rampart
#

Did you #define raindrops?

sturdy cedar
#

In .h>

#

?

unreal rampart
#

The define and variable name need to be different

sturdy cedar
#
#if !defined(RAINDROPS_H)
#define RAINDROPS_H
#include <string>
using namespace std;

namespace raindrops {
    string rain(int RAINDROPS_H);
    string convert(int RAINDROPS_H);
}  // namespace raindrops

#endif // RAINDROPS_H```
#

This is my .h file

unreal rampart
#

The function definitions cannot use that macro

sturdy cedar
#

Changed it in .h as well

#
#if !defined(RAINDROPS_H)
#define RAINDROPS_H
#include <string>
using namespace std;

namespace raindrops {
    string rain(int raindrops);
    string convert(int raindrops);
}  // namespace raindrops

#endif // RAINDROPS_H```
#

same error still

unreal rampart
#

Can you paste the error?

sturdy cedar
#
/tmp/raindrops/raindrops.cpp:18:42: error: a function-definition is not allowed here before '{' token
   18 |             string convert(int raindrops){
      |                                          ^
make[2]: *** [CMakeFiles/raindrops.dir/build.make:90: CMakeFiles/raindrops.dir/raindrops.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:85: CMakeFiles/raindrops.dir/all] Error 2
make: *** [Makefile:91: all] Error 2```
unreal rampart
#

Oh. You're defining that function inside another function?

sturdy cedar
#

Yes

#

inside rain

unreal rampart
#

Why? I don't think that is valid

sturdy cedar
#
#include "raindrops.h"
#include <string>
using namespace std;

namespace raindrops {
    string rain(int raindrops){
        string result = "";
        if(raindrops % 3 == 0){
            result+= "Pling";
        }
        if(raindrops % 5 == 0){
            result+= "Plang";
        }
        if(raindrops % 7 == 0){
            result+= "Plong";
        }
        else{
            string convert(int raindrops){
                result+= to_string(raindrops);
                return result;
            } 
        }
        return result;
    }

    
}  // namespace raindrops```
#

Bcoz in the last else part that's where I would need output of that function

unreal rampart
#

You should declare outside the function and call it there

sturdy cedar
#

Oh so this way of function inside func doesnt work?

unreal rampart
#

That's what the error says

#

You never call the function. And it's not defined where the header says it is.

sturdy cedar
#
#include "raindrops.h"
#include <string>
using namespace std;

namespace raindrops {
    string result = "";
    string convert(int raindrops){
                result+= to_string(raindrops);
                return result;
            } 

    string rain(int raindrops){    
        if(raindrops % 3 == 0){
            result+= "Pling";
        }
        if(raindrops % 5 == 0){
            result+= "Plang";
        }
        if(raindrops % 7 == 0){
            result+= "Plong";
        }
        else{
                convert(raindrops);
            } 
        }
        return result;
    }

}  // namespace raindrops
#
/tmp/raindrops/raindrops.cpp:26:9: error: no return statement in function returning non-void [-Werror=return-type]
   26 |         }
      |         ^
/tmp/raindrops/raindrops.cpp: At global scope:
/tmp/raindrops/raindrops.cpp:27:9: error: expected unqualified-id before 'return'
   27 |         return result;
      |         ^~~~~~
/tmp/raindrops/raindrops.cpp:30:1: error: expected declaration before '}' token
   30 | }  // namespace raindrops
      | ^
cc1plus: all warnings being treated as errors
make[2]: *** [CMakeFiles/raindrops.dir/build.make:90: CMakeFiles/raindrops.dir/raindrops.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:85: CMakeFiles/raindrops.dir/all] Error 2
make: *** [Makefile:91: all] Error 2```
#

I declared both functions in .h file

#

Defined both in .cpp

#

Wait

unreal rampart
#

Your final else has an extra }

sturdy cedar
#

Errors solved but only 1 testcase passed

#

Yes

#



FAILED:
  REQUIRE( "Plang" == raindrops::convert(5) )
with expansion:
  "Plang" == "135"
at /tmp/raindrops/raindrops_test.cpp:19```
#

Only testcase for 1 passed

#
  REQUIRE( "Plang" == raindrops::convert(10) )
with expansion:
  "Plang" == "135768910"
at /tmp/raindrops/raindrops_test.cpp:39```
#

Shouldn't convert function just be converting integer to string?

#

for concatenation further

unreal rampart
#

You have a result string defined in your namespace. That's probably messing things up. It can get added to with each test and isn't ever reset

sturdy cedar
#

So what should I do for that

unreal rampart
#

Ideally not define variables outside of functions

sturdy cedar
#

Then define it empty inside both funcs?

#

Even then just 3/18 passed

#
  REQUIRE( "Pling" == raindrops::convert(3) )
with expansion:
  "Pling" == "3"
at /tmp/raindrops/raindrops_test.cpp:15
#
  REQUIRE( "Plong" == raindrops::convert(14) )
with expansion:
  "Plong" == "14"
at /tmp/raindrops/raindrops_test.cpp:43
#
#include "raindrops.h"
#include <string>
using namespace std;

namespace raindrops {

    string rain(int raindrops){   
        string result = "";
        if(raindrops % 3 == 0){
            result+= "Pling";
        }
        if(raindrops % 5 == 0){
            result+= "Plang";
        }
        if(raindrops % 7 == 0){
            result+= "Plong";
        }
        else{
                convert(raindrops);
            } 
        return result;
    }

            string convert(int raindrops){
                string result = "";
                result+= to_string(raindrops);
                return result;
            } 

}  // namespace raindrops
#

So it has worked properly for non-factors

#

but not factors

#
  REQUIRE( "Plang" == raindrops::convert(3125) )
with expansion:
  "Plang" == "3125"
at /tmp/raindrops/raindrops_test.cpp:79```
#

Convert function should be converting integer to sound?

unreal rampart
#

Yes. It returns a string version of the number.

#

What do you do with that string?

sturdy cedar
#

Return ?

unreal rampart
#

You can convert which returns a value.

#

Are you supposed to do something with it?

sturdy cedar
#

Im converting to sound inside rain function

#

If I do that in convert func what will rain func do?

unreal rampart
#

You're calling a function which returns a value.

#

The results string in raindrops doesn't change when you do that

sturdy cedar
#

results string in the else part function is getting appended by the non-factored number

#

and it returns the string of that integer

#

I'll redo

#
#include "raindrops.h"
#include <string>
using namespace std;

namespace raindrops {

    string rain(int raindrops){   
        string result = "";
        if(raindrops % 3 == 0){
            result+= "Pling";
        }
        if(raindrops % 5 == 0){
            result+= "Plang";
        }
        if(raindrops % 7 == 0){
            result+= "Plong";
        }
        else{
                convert(raindrops);
            } 
        return result;
    }

            string convert(int raindrops){
                string result = "";
                result+= to_string(raindrops);
                return result;
            } 

}  // namespace raindrops
#

Old for reference

#

redoing new

unreal rampart
#

The result variables in the two functions are two unrelated variables. Adding to one does not change the other. Each function has independent variables.

sturdy cedar
#

Okay I've rewritten the code with 15/18 testcases passed

#
#include "raindrops.h"
#include <string>
using namespace std;

namespace raindrops {

    string convert(int raindrops){
        string result = "";
        if(raindrops % 3 == 0){
            result += "Pling";
        }
        if(raindrops % 5 == 0){
            result+= "Plang";
        }
        if(raindrops % 7 == 0){
            result+= "Plong";
        }
        else{
            rain(raindrops);
        }
        return result;
    }

    string rain(int raindrops){
        string result = "";
        result+= to_string(raindrops);
        return result;
    }
}  // namespace raindrops
#

Testcases failed are:

  REQUIRE( "1" == raindrops::convert(1) )
with expansion:
  "1" == ""
at /tmp/raindrops/raindrops_test.cpp:10

FAILED:
  REQUIRE( "8" == raindrops::convert(8) )
with expansion:
  "8" == ""
at /tmp/raindrops/raindrops_test.cpp:31

FAILED:
  REQUIRE( "52" == raindrops::convert(52) )
with expansion:
  "52" == ""
at /tmp/raindrops/raindrops_test.cpp:71```
#

1
2^3 = 8
52

#

Okay so now its working for factors but not for non-factors

late helm
#

Let's focus on the first test:

FAILED:
  REQUIRE( "1" == raindrops::convert(1) )
with expansion:
  "1" == ""
at /tmp/raindrops/raindrops_test.cpp:10

It calls convert(1) and expects the result to be "1".
But the function returns "", an empty string.
The function convert() only has a single return, so this return result;at the end seems to return an empty string, result must be empty for some reason.
How can that be? Try to simulate the function in your head. Where do you think result should have been modified? why did that not happen?

sturdy cedar
#

Okay so

#

The else part didnt get executed

#

The reason for that is I think that it's connected to the if part of %7

#

And since it couldn't enter into any condition

#

It returned our starting defined variable result as empty string

#

I got it working, 18/18 but unsure about if I'm correct when I was using else above

#
#include "raindrops.h"
#include <string>
using namespace std;

namespace raindrops {

    string convert(int raindrops){
        string result = "";
        if(raindrops % 3 == 0){
            result += "Pling";
        }
        if(raindrops % 5 == 0){
            result+= "Plang";
        }
        if(raindrops % 7 == 0){
            result+= "Plong";
        }
        if(result.empty()){
            result = rain(raindrops);
        }
        return result;
    }

    string rain(int raindrops){
        string result = "";
        result+= to_string(raindrops);
        return result;
    }
}  // namespace raindrops
unreal rampart
#

If it passes the tests, it's usually mostly correct

#

This code looks correct to me, but there is room for improvement. You can request a code review to further improve the solution, though!

#

We ask that people react to the top/original post with a ✅ (:white_check_mark:) if everything is resolved. This indicates to others that this issue has been resolved and locks the thread.