C++ Coding Assistance

I currently have a program I am to be designing, although I'm new to the field I in no way intend to "leech off of" or fall into the laps of anyone. I simply wish to find someone with time to help me with some of my code. By this I mean in terms of giving probably hints or telling me if I'm going off-track. Currently I need help with a function using a class and as well a relatively complex use of the Modulo operator.
If needed I can provide a sample of the code I've struggled to write so far.
Again I'm not asking for anyone to derive me of my experience but rather act as a guide through the parts I find relatively difficult due to being at an introductory level of the language.
You'll certainly need to provide sample code.

Can't help out if we can't see it. :-)
Are you looking for modulo operator's function ?

1
2
3
4
5
6
7
8
9
int mod (int v1, int v2)
{
   if(v2 < 0) //you can check for v2 == 0 separately and do what you want
     return mod(-v1, -v2);   
   int ret = v1 % v2;
   if(ret < 0)
     ret+= v2 ;
   return ret;
}


Or following one ...
1
2
3
4
5
6
7
template< class IntType >
IntType mod( IntType v1, IntType v2)
{
    // take care of negative value of v2
    IntType const r = v1%v2;
    return (r < 0? r + v2 : r);
}


Or if you are looking for modulo operator overloading ....
1
2
3
4
5
class Test
{
public:
     float operator%(float);
};
Ok Im supposed to create a function that inputs a 6-digit number and returns each digit multiplied by 2 and separated by three spaces ( Given the hint that we should use modulu and integer division to return the digit)
Currently having a few errors but here's what I have so far. Again just looking for insight where possible.


>header file

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
#include <iostream>

using namespace std;

#ifndef FUN_H
#define FUN_H

class Fun

{

public:

    Fun(long int)
    {



        seperator(long newNumber);
    };



    void seperator(long int newNumber)
    {
       int Remainder;

      Remainder=newNumber%2;



    }
    ;};


private://Member Variables

   long int newNumber;
};//End of Class


#endif


>.cpp file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

#include "fun.h"

    Fun Tobeshown;

    long int number;

    cout<<"Hello, please enter a six digit number! ";

    cin>>number;

cout<<Tobeshown.seperator(long newNumber);
    return 0;
}



Error

C:\Users\Closetgeekz\Programset0\Nich-build-desktop-Qt_4_8_1_for_Desktop_-_MinGW__Qt_SDK__Debug\..\Nich\fun.h:19: error: expected primary-expression before 'long'
C:\Users\Closetgeekz\Programset0\Nich-build-desktop-Qt_4_8_1_for_Desktop_-_MinGW__Qt_SDK__Debug\..\Nich\fun.h:-1: At global scope:

C:\Users\Closetgeekz\Programset0\Nich-build-desktop-Qt_4_8_1_for_Desktop_-_MinGW__Qt_SDK__Debug\..\Nich\fun.h:36: error: expected unqualified-id before 'private'
C:\Users\Closetgeekz\Programset0\Nich-build-desktop-Qt_4_8_1_for_Desktop_-_MinGW__Qt_SDK__Debug\..\Nich\fun.h:39: error: expected declaration before '}' token






on line 19 remove the long from the function call
remove line 33


C:\Users\Closetgeekz\Programset0\Nich-build-desktop-Qt_4_8_1_for_Desktop_-_MinGW__Qt_SDK__Debug\..\Nich\funn.cpp:7: error: no matching function for call to 'Fun::Fun()'
C:\Users\Closetgeekz\Programset0\Nich-build-desktop-Qt_4_8_1_for_Desktop_-_MinGW__Qt_SDK__Debug\..\Nich\funn.cpp:13: error: expected constructor, destructor, or type conversion before '>>' token
C:\Users\Closetgeekz\Programset0\Nich-build-desktop-Qt_4_8_1_for_Desktop_-_MinGW__Qt_SDK__Debug\..\Nich\funn.cpp:17: error: expected declaration before '}' token




This seems to be my current issue at the moment now, and I am honestly sorry for the line 33 error, I believe I had another function around there that I removed.
sigh... plz repost ur code
zero117 wrote:
Ok Im supposed to create a function that inputs a 6-digit number and returns each digit multiplied by 2 and separated by three spaces

A function, by nature, returns one value. Are you actually wanting to return these values or just print them? You could return a vector containing the new values or something like that but I'm not sure if that's what you need.

The algorithm is a relatively simple one. You know that you have a six digit number, so your initial divisor will be 100000. Getting each digit is as dividing by the divisor, reassigning your number using the modulus operator on the divisor then dividing the divisor by 10. I could write up the code for this, but you ought to wrap your head around it yourself.

From there, multiplying by two and printing separated by three spaces should be bread and butter.
Basically it's one function that should take each individual digit from the 6-digit number entered and return the values multplied by 2 and three spaces apart. I can more or less see how the math of that will work. I'm simply asking for assistance in the fluency of my code so far, I went as far as making the program able to print the numbers entered, but setting it up for the modulu operation is my problem. Moreover I'm just having general problem with classes since we mainly covered classes with strings and numbers. very sorry if I have confused and or caused confusion at this point. Here is my current code

Header
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


#include <iostream>

using namespace std;

#ifndef FUN_H
#define FUN_H

class Fun

{

public:

    Fun(long int)
    {



        seperator(newNumber);
    };



    void seperator(long int newNumber)
    {
       int Remainder;

      Remainder=newNumber%2;

}


private://Member Variables

   long int newNumber;
};//End of Class


#endif











.Cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21


#include <iostream>
using namespace std;

#include "fun.h"

    Fun Tobeshown;

    long int number;

    long int Remainder;

    cout<<"Hello, please enter a six digit number! ";

    cin>>number;

cout<<Tobeshown.seperator(long newNumber);
    return 0;
}


Advice on the constructor would be appreciated as well, I was content with the idea of there being a default constructor, however the introduction of a overload constructor caused some confusion.
im going to clean up your code and document whats going wrong give me a sec
Last edited on
If it needs to be set up in a class, I'd go for something like this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <vector>

class MyClass
{
private:
   int initial_num;  // To store the six digit value
   vector<int> processed; // To store each digit after processing
public:
   MyClass(int n): initial_num(n){} // ctor
   void Process() {  /* Do your math inside this function */ }
   void Print() { /* Print values in this function */ }
};

int main(int argc, char* argv[])
{
   MyClass A(123456);
   A.Process();
   A.Print();
}


I'd perform the modulus/division operations in the Process function and add each result to the vector. Then, in Print, I'd iterate through the vector and print each value.
header
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
#ifndef FUN_H
#define FUN_H

#include <iostream>

class Fun
{
	public:
		Fun(long int)
		{
			seperator(newNumber);
    		}

		void seperator(long int newNumber)
    		{
       			int Remainder;
			Remainder=newNumber%2;
		}
};

#endif

/*changes:
-removed namspace std reference because u dont need it here
-cleaned up white spaces
-removed the private section because it just contains a variable that was already declared in seperator
-removed the semicolon from fun: its not neccesary
*/


cpp file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "fun.h"

using namespace std;

int main()
{
	Fun Tobeshown;
	long int newNumber;
	
	cout<<"Hello, please enter a six digit number! ";
	cin>>number;

	cout<<Tobeshown.seperator(number);

	return 0;
}

/*changes:
-removed iostream since it was already declared in fun.h
-moved fun.h to top
-removed long int remainder becuase it wasnt being used
-changed the newNumber in seperator to number because you don't have a variable called newNumber
*/
@Aramil of Elixia / OP (if applicable) - A class without member variables seems pointless to me.
i wasnt rewriting it i was just removing errors and cleaning it up, but i do agree. i removed hos though because it wasnt doing anything
Got many errors when I popped that in, but considering what you've all told me so far and that clean up you gave my code I'm going to restart the entire thing. Hopefully I don't make senseless declarations again and come back with a stronger issue.
Ok I rewrote the code, again I'm just learning so here's what I have (still seeing how I can get the individual digits out using the separator, I just placed the %(some number) there to check if it was working.

Header
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

#include <iostream>

using namespace std;

#ifndef FUN_H

#define FUN_H


class Fun
{
public:

    Fun()//Default Constructor
    {
      newNumber=0;
    }

    Fun(long int number)//Overload Constructor(sets newNumber to Number entered)
    {
        newNumber=number;

    }
    ~Fun(){}//Destructor



    void separator(long int newNumber)
    {
        long int results;

        results=newNumber%3;

    }


    long int  getresults()
    {
        return results;
    }

private:
        //Member Variable
        long int newNumber;

        long int results;


};

#endif




.CPP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

#include "fun.h"

int main()
{

    long int number;

    ;

    cout<<"Please enter a 6 digit number:";

    cin>>number ;

    Fun Results(number);

    cout<<endl<<Results.getresults()<<endl;

    return 0;
}
I'd like to ask if I'm allowed to make all the necessary calculations after the "results=" function. As in if there I can go about doing my harsh calculations( and yes we were told we'd be left with a messy pile of code but it's ok). Thanks for any consideration.
Also for every number I pop in I get a "result" of 582. Why is this?
Last edited on
And I'm Honestly stuck here now.Why isn't results being stored as the mod of newNumber.
Last edited on
Finished , thanks for any consideration.
Topic archived. No new replies allowed.