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.
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 ....
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.
#include <iostream>
usingnamespace std;
#ifndef FUN_H
#define FUN_H
class Fun
{
public:
Fun(longint)
{
seperator(long newNumber);
};
void seperator(longint newNumber)
{
int Remainder;
Remainder=newNumber%2;
}
;};
private://Member Variables
longint 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>
usingnamespace std;
#include "fun.h"
Fun Tobeshown;
longint 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
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.
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
#include <iostream>
usingnamespace std;
#include "fun.h"
Fun Tobeshown;
longint number;
longint 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.
#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.
#ifndef FUN_H
#define FUN_H
#include <iostream>
class Fun
{
public:
Fun(longint)
{
seperator(newNumber);
}
void seperator(longint 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
*/
#include "fun.h"
usingnamespace std;
int main()
{
Fun Tobeshown;
longint 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
*/
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.
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.