Error: expected initializer before '%' token

I need a little help figureing out why I am getting an error in line 9.

#include <iostream>

using namespace std;


int devideNumbers (int a, int b){
int devideNumbers( int c, int d);
int devideNumbers (int e, int f);
int answer = a % b, c % d, e % f;
return answer;

}


int main( ){

cout << devideNumbers (23,10);
cout << devideNumbers (345, 100);
cout << devideNumbers (8923, 1000);

return 0;

}
Firstly, this is a horrific mess with an ugly recursion that I'm sure you didn't mean.

What exactly do you think this will achieve?

1
2
int devideNumbers (int a, int b){
    int devideNumbers( int c, int d);


The function calls itself as soon as it starts, and then that call will call itself, and then that call will call itself, on and on and on forever and ever until you run out of memory and it crashes.

int answer = a % b, c % d, e % f;
What do you think this is going to do? It's horrific, especially so in the context of your program.

I think you meant to write this code:

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

using namespace std;

int devideNumbers (int a, int b){
  int answer = a % b;
  return answer;
}

int main( ){
  cout << devideNumbers (23,10);
  cout << devideNumbers (345, 100);
  cout << devideNumbers (8923, 1000);

  return 0;
}




I am a novice at C++ and am taking an online course.

I am trying to finish re-doing an assignment I got a 48 on.

To answer your question What exactly do you think this will achieve? I am not really sure what I am doing but I am trying my best.

The directiosn are kinda hard for me to understand... idk if they are written bad or I am just NOOB

C++ Program

Write a program that prints to cout, separated by a space, the digits of any three digit integer. You will need one variable, named, for example, value, for the number (int value) to be broken down into its digits. You might also need other variables to store the digits as you get them.

The structure of your program could be:

#include <iostream>

int main()

{ int number;

int digit1, digit2, digit3; // place holders for the digits.

cout << "Type a three digit number\n";

cin >> number;


// your code here


return 0;

}

Hint: You can do this by using repeated divisions by 10, and remembering that dividing two integers always resuts in an integer. For
example 6/5 would yield a result of 1. You can use the % operation

If you input a number like 345 your ouput should be 3 4 5 (its three digits separated by a space)

Last edited on
What exactly do you think this will achieve? I am not really sure what I am doing but I am trying my best.


Okay, stop there. Programming is unforgiving. The compiler will do exactly what you tell it to, so you need to know what everything you type does. If you don't know what you told it to do, the chances of it doing what you want are very slim. Guessing is a very bad strategy in programming. If you are not sure, you need to get sure.

It looks like you're not expected to be using functions for this.

So start with the code they give you:


1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

int main()
{ int number;
  int digit1, digit2, digit3; // place holders for the digits.
  cout << "Type a three digit number\n";
  cin >> number;

  // your code here
  return 0;
}



Right, you need to write some code that starts with number, which is an int, and splits it into three separate numbers.

You've been told that the % operator might be helpful. Tell me, given what they've told you about the % operator, what do you think would be the result of

345 / 100 ?
Last edited on
I understand the / gives you the quotient, and % gives you the remainder.

He also sent out a hint.

Some useful hints as to how division by 10, 100, 100, etc,and remainders (%) can be used to isolate digits.
I will just show some examples, for you to draw your own conclusions:
In C++

23/10 is 2

435/100 is 4

8923/1000 is 8

this happens because division of two int is also an int. Decimal results are obtained if at least one of the operands is a decimal (float or double).

See the section on types, promotion, casting, float, double and int.

As for the remainder operator:

45%10 is 5 (there remainder of 45 divided by 10)

732 % 10 is 2

The above should be enough to point you in the right direction. bins how to isolate the middle digit of the three digit number, by combining the above hints.


435/100 is 4. That's how to get the first digit of a three digit number - divide by 100.

So, once we do that, how can we get the next digit? We can take that 4, and subtract 4*100 from the original number, and we get...

435 - (4*100) = 35

That's a two digit number. Hmmm. How can we get the first digit? Let's look at the hints again...

So 23/10 is 2.... hey, that's the first digit! Great. That's how to get the first digit of a two digit number - divide by ten. Great.

So we can get the first digit of 23 by dividing by ten!

So here's where we are:

1
2
3
4
5
int firstDigit = number / 100;
// now convert number into two digits
number = number - (firstDigit * 100);
// now get second digit
int secondDigit = number / 10;


I'll leave you to work out getting the third digit.
So I have working on this. I am not quite sure where you were going with this Moschops, but this is what my present progress looks like...

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{ int numbers;
int digit1 = 23 / 10;
int digit2 = 435 / 100;
int digit3 = 8923 / 1000;

cout << digit1 << " " << digit2<< " " << digit3 <<" ";
cin >> numbers;

system ("PAUSE");
return 0;
}

How do you upload the small blue text box? instead of my copy and paste jive?
Last edited on
I was going this way:

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;

int main()
{ int number;

cout << "Enter a three digit number" <<endl;
cin >> number;

int firstDigit = number / 100;
// now convert number into two digits
number = number - (firstDigit * 100);
// now get second digit
int secondDigit = number / 10;
// now get third digit
int thirdDigit = number - secondDigit*10;

 cout << "The three digits are " << firstDigit << " " << secondDigit << " " << thirdDigit << endl;
return 0;
}


To mark code, put [/code] after the code and [code] before the code.
I am taking my C++ class online, I wish the class was in person so I could get one on one help from my teacher.

I think my inability to figure out this program has to do with my poor math skills.

Topic archived. No new replies allowed.