new to c++ counting numbers +

heolp help!
i'm new to c++ and i need help to write a new program
i guess a simple one that counts numbers would be good
i know printf() and some other functions


1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>

int main()
 {
   int y = 0;

   need more stuff here :)

   printf("\nthe number in y is %d\n", y);
   
   return 0;

 }


thanks for your help!
would putting an array of numbers like this work?

int y[] = { 5, 10, 22, 27, 28, 09, 11, 30 };

this a valid array?

if so i may be able to loop it to add and print
int y[] = { 5, 10, 22, 27, 28, 9, 11, 30 };

this is better

got an octal error when 9 was listed as 09 :(
Last edited on
i'm new to c++


Your initial code is C programming. I am exactly not sure what are you trying to do. However, below is a C++ style code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

#include <iostream> // Not <stdio.h>

int main()
{

    int y[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    int runningTotal{ 0 };

    std::cout << "Adding all the elements in the array 'y'...\n"; // cout, not printf.

    for (auto count : y) // Use a range for loop to count all the elements in the array (C++ 11)
    {
	   std::cout << count << std::endl;
	   runningTotal += count;
    }

    std::cout << "The total is " << runningTotal << std::endl;

}


Also, take a look at the tutorials in the link below:
http://www.cplusplus.com/doc/tutorial/

It could give you a better idea. Good luck!
cout is good to
both are standard to c++

i see printf() listed in cstdio.h
as part of the current standard c11
i really like printf() even though many people
like using the cout of c++

http://www.cplusplus.com/reference/cstdio/printf/

the example you put looks good and effective

thanks for help
yes tutorial link good place to start

need good format more than luck i think
good format the way to make good program
Last edited on
If you are interested in C++, I would recommend you the book
"Programming Principles and Practice Using C++"
It contains the reasons on why C++ programmers use <iostream>, instead <stdio> with certain exceptions, of course.
oh thank you thank you
maybe at barnes and noble?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>

int main()
{
  
   int y[] = { 4, 6, 11, 12, 23, 42, 24, 16 };
   int p;
   int o;

   o = y[0];

   for (p = 1; p < 8; p++)
     {
        o = y[p] + o;
     }

   printf("\n\nThe amount of y[] unsized array elements added is %d\n\n", o);

   return 0;

}



i worked out this one :)
this look good?

maybe i try a cout version later
Last edited on
Topic archived. No new replies allowed.