Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have an info task to write a prog in C that enters 2 numbers and check if a is divided by b . So for one scenario it must run as " a is divided by b " is the 2nd must run as " imposibble operation " (25 divided by 0 ) and the third that " Number must be different then 0 " ( 0 divided by 25) . I m new in class and i really dont know how to write it can someone help me ?

What I have tried:

i tried so hard and got so far
C
#include <iostream>
#include <string>   
  
  int nr1,nr2;
  scanf("%d,%d",&nr1,&nr2);
	if(nr1%nr2==0)
		printf("%d is divisible by %d" , &nr1 ,&nr2);
		{
	if(nr1%0)
	printf("Impossible operation");
		}
		if(0%nr1)
		printf("The num must be different then 0");
	{
    return 0;
i got those errors:
__tester__.cpp:5:8: error: expected constructor, destructor, or type conversion before '(' token
   scanf("%d,%d",&nr1,&nr2);
        ^
__tester__.cpp:6:2: error: expected unqualified-id before 'if'
  if(nr1%nr2==0)
  ^~
__tester__.cpp:8:3: error: expected unqualified-id before '{' token
   {
   ^
__tester__.cpp:12:3: error: expected unqualified-id before 'if'
   if(0%nr1)
   ^~
Posted
Updated 31-Oct-22 7:11am
v2
Comments
k5054 31-Oct-22 12:27pm    
Show us your work! We'll be glad to help you with the parts you are stuck on, but you need to show us how far you've got and what problems you're facing.
CHill60 31-Oct-22 12:40pm    
OP responded via a solution. I have moved it into their question. They asked you not to laugh too
Bogdan Alexandru Oct2022 31-Oct-22 13:00pm    
?
CHill60 1-Nov-22 6:12am    
You posted a solution with your code - you should have put your code into your question. I did that for you. I then deleted your solution so that your question appeared in the list of Unanswered questions.
With your code you posted the words "ok, but don't laugh".
All I have done with the comment above is let k5054 know that you had responded to their comment, that I moved the code into the question and that you asked them not to laugh.
Does that answer your "?"
Bogdan Alexandru Oct2022 31-Oct-22 12:59pm    
what?

Those are called syntax errors: the compiler does not understand what you wrote, so it is trying to explain to you what you did wrong.
So start by looking at the error message, and at your code:
__tester__.cpp:5:8: error: expected constructor, destructor, or type conversion before '(' token
   scanf("%d,%d",&nr1,&nr2);
That code - and the lines above it - is not inside a function declaration - and in C all executable code must be part of a function.
Probably, you wanted this:
C
#include <iostream>
#include <string>   
int main()
   {  
   int nr1,nr2;
   scanf("%d,%d",&nr1,&nr2);
   ...

Fix that, and compile again. You will get different errors, becuase that code is full of syntax errors!

So read this: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^] and start looking error messages. Fix them one by one, and compile after each fix. You'll get there!

You should expect to get syntax errors every day, probably many times a day while you are coding - we all do regardless of how much experience we have! Sometimes, we misspell a variable, or a keyword; sometimes we forget to close a string or a code block. Sometimes the cat walks over your keyboard and types something really weird. Sometimes we just forget how many parameters a method call needs.

We all make mistakes.

And because we all do it, we all have to fix syntax errors - and it's a lot quicker to learn how and fix them yourself than to wait for someone else to fix them for you! So invest a little time in learning how to read error messages, and how to interpret your code as written in the light of what the compiler is telling you is wrong - it really is trying to be helpful!
 
Share this answer
 
Comments
Bogdan Alexandru Oct2022 31-Oct-22 13:09pm    
ok thanks for advices i will read that
OriginalGriff 31-Oct-22 13:32pm    
You're welcome!
First, your program needs to begin with a main statement. Something like :
C++
int main()
{
    return 0;
}
That is pretty as minimal a program as is possible. You can add logic and functions as necessary.

Then you need to write your if statements and braces so they make logical and functional sense. See this page for a tutorial on the topic : Tutorial : Statements and Flow Control[^]. The "cout <<" statements are essentially the same as printf statements.
 
Share this answer
 
v2
Some notes:
1) You seem to be writing C, but you're including C++ headers. Decide which language you are actually targeting and use the appropriate headers. For C that would be <stdio.h>. For C++, if you want to use printf() and scanf() you'll need to #include <cstdio>

2)Every C or C++ program needs a main() function. e.g.
C
#include <stdio.h>

int main()
{
    printf("Hello World!\n");
    
    return 0;
}


3) Think about the order of your tests. What happens if nr2 is Zero when you try to do nr1 % nr2 first?

4) Chose an indentation style and stick with it. It will help you identify where you might have made programming errors

5) You seem to be unsure of how curly brackets { } work. Every { must have a matching }. Looking at the code you've posted above you have an orphaned { just before the return statement. There seems to be other issues here with curly brackets, too -- see item 4) for a possible way to help resolve this

6) In a couple of places, I think you meant to test for equality == instead of doing modulus operations %.
 
Share this answer
 
Comments
OriginalGriff 31-Oct-22 13:34pm    
It's C++ - look at the error messages, and they reference a file called "__tester__.cpp"

Chances are he doesn't know what the difference is!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900