Please Help

Hello all, new to C++ programing here. Hope someone can take a peak at my code here and show me what i am doing wrong. Hope you guys get a good laugh, but also can help me out, and include what i did wrong please.

#include <stdio.h>
int main()

{

int array1[6];
int sum=0,i,n,s,w;

printf("Enter 6 numbers with the first number being the number to be summed up\n");
printf("Enter the elements in ascending order after the number to be summed\n");
for (i=6; i<1; i--)
scanf(&w[i];);

for (i=6; i>0; i--)
sum=0
sum +=&w[1];
if(sum != &w[6])

{
Sum +=&w[2]
}
Else(printf("the solution is &w[1] is equal to the number entered"))
if (sum != &w[6])

{
Sum=+&w[3]
}
Else (printf("the solution is &w[1] + &w[2] is equal to the number entered"))
if (sum != &w[6])
{
Sum+=&w[4]
}
Else (printf("The solution is &w[1] + &w[2] + &w[3] is equal to the number entered"))

if (sum !=&w[6])
{
Sum+=&w[5]
}
Else (printf("The solution is &w[1] + &w[2] + &w[3] + &w[4] is equal to the number entered"))

if (sum == &w[6])
{
printf ("The solution is &w[1] + &w[2] + &w[3] + &w[4] + &w[5] is equal to the number entered")
}

Else (printf("There is no solution with the values entered, please try again."))

}
Hi there.

There are a number of things that jump to mind first, and these aren't even solely C++ problems, but basic programming rules :)

To organize each case better I'll number each one and reference lines of code if necessary.

1. You have declared int main (). Make sure that if you declare a function with a certain return type, that it actually returns the type you have declared it with. To clarify, since you declared int main(), main should return an integer! Best to stick with return 0;. Using 0 instead of any other number is generally good practice. A 0 represents successful execution, and 1 being an unsuccessful execution, possibly involving errors.

2. Your nested for-loops must have brackets around their bodies. Get in the habit of using brackets for logic statements and decision statements (if, else if, etc). In C++ you can only skip brackets if the body is 1 line.
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Take for instance these blocks, which assume you have
// some sentinel variable of num to which i counts to before
// stopping execution. In the body are some generically-named
// functions that do something as i increments. Pretty simple idea.

for (int i = 0; i < num; i++)
{
	FunctionA(i);
	FunctionB(i);
	FunctionC(i);
}// Need brackets for multiple-line body

for (int i = 0; i < num; i++)
	FunctionA(i);
// Don't need brackets for single-line body 


3. You are referencing w like Sum=+&w[3].
There are a couple issues here. An operator like "plus-assign" or += always has the assignment operator appearing after the addition, subtraction, concatenation, or whatever. You got it right other times though with your != and other +='s.

The other thing is that I think you're trying to reference w like an array...only it isn't an array. It's defined in your code near the start of main as an int. Also when accessing an element in an array, use array_name[index]. By including the [index] you dereference the element at the given index location, so your ampersand & is unnecessary.


I think that's a good start for you to look at and start to work with. Beware though, this response is coming from a tired, full-time dude that should be going to bed about now. I could be blowing steam, hopefully if I do someone else will set you straight.

Cheers.
Last edited on
ENIGMAx, Thank you bud.... Awesome explanation. I really do appreciate it buddy.
Topic archived. No new replies allowed.