Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello today i just started my optional C programming language in CS because many have told me that is good to start with C. I have a homework to do a small program with 2 digits and to check if one of them or their sum is 10 to print me " one number or their sum is 10 " or else print " none of them or their sum is 10 ". I wrote this in codeblocks

#include <stdio.h> 
#include <string>
int sum , n1 , n2; 
scanf("%d %d" , &n1 , &n2);
if((n1==10) || (n2==10))
printf("A number or their sum is 10");
sum=n1+n2;
if(sum==10)
printf("A number or their sum is 10")
{
    else if 
    printf("Numbers or their sum is not 10");
}
return 0;
}


i get some errors :

Syntax Error(s)
__tester__.cpp:4:6: error: expected constructor, destructor, or type conversion before '(' token
 scanf("%d %d" , &n1 , &n2);
      ^
__tester__.cpp:5:1: error: expected unqualified-id before 'if'
 if((n1==10) || (n2==10))
 ^~
__tester__.cpp:7:1: error: 'sum' does not name a type
 sum=n1+n2;
 ^~~
__tester__.cpp:8:1: error: expected unqualified-id before 'if'
 if(sum==10)
 ^~
__tester__.cpp:14:1: error: expected unqualified-id before 'return'
 return 0;
 ^~~~~~
__tester__.cpp:15:1: error: expected declaration before '}' token
 }
 ^


What I have tried:

i searched them on google but dont found something really helpfull. Sorry if i m bad i m new to C
Posted
Updated 14-Nov-22 3:25am
Comments
Pascal Vlad 14-Nov-22 8:27am    
__tester__.cpp: In function 'int main()':
__tester__.cpp:10:1: error: expected ';' before '{' token
{
^

You must have a main function, see example here:
How to create and run C program using CodeBlocks - Codeforwin[^]

Note that the following is C specific code and won't work in C++:
#include <stdio.h> 
#include <string.h>

For C++ see:
How to Download Code Blocks and Write a Hello World Program in C++ : 50 Steps - Instructables[^]
 
Share this answer
 
v4
Comments
Pascal Vlad 14-Nov-22 8:27am    
__tester__.cpp: In function 'int main()':
__tester__.cpp:10:1: error: expected ';' before '{' token
{
^
RickZeeland 14-Nov-22 8:34am    
and what happens when you try the hello world example?
C console programs always start in the main function, so a minimal C app would look like this:
C
#include <stdio.h>

int main()
    {
    printf("Hello World\n");
    return 0;
    }
All executable code except global variable definitions (with or without simple assignments) must be inside a function.
So your line:
C
int sum , n1 , n2; 
could be outside any function (but in this case is probably not a good idea as they dont; need to be global variables) but no other lines can be.
It's also a very good idea to indent your code - it makes it a lot more obvious what you expect to happen, and to match up brackets.
In addition, it's a good idea to always use curly brackets in if, else, and loops - particularly when you are just getting started.

So try like this:
C
#include <stdio.h> 
int main()
    {
    int sum , n1 , n2; 
    scanf("%d %d" , &n1 , &n2);
    if ((n1 == 10) || (n2 == 10))
        {
        printf("A number or their sum is 10");
        }
    else
        {
        sum = n1 + n2;
        if (sum == 10)
            {
            printf("A number or their sum is 10")
            }
        else 
            {
            printf("Numbers or their sum is not 10");
            }  
        }
    return 0;
    }
 
Share this answer
 
Quote:
__tester__.cpp:4:6: error: expected constructor, destructor, or type conversion before '(' token

Why .cpp? You are supposed to write a C (.c) program.
Try
C
#include <stdio.h>
  
int main()
{
  int n1 , n2;

  scanf("%d %d" , &n1 , &n2);

  if( (n1 == 10) || (n2 == 10) || ((n1+n2) == 10))
    printf("One number or their sum is 10\n");
  else
    printf("None of them or their sum is 10\n");

  return 0;
}
 
Share this answer
 

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