473,387 Members | 1,476 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Problems with my code

Hunderpanzer
Hi, I'm pretty new here. I've started programming C++ on and off now for a while now, but never got anywhere with it. So I decided to stick with my book examples and see where it takes me.

Okay my goal is to write a program that prompts the user for 7 values, stores them in an array, and then prints each of them out, and their sum.
It also notes that I need two for loops to be included; one for collecting the inputs, and one for calculating the sum and printing the value.

This is what I have so far

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <math.h>
  3. using namespace std;
  4.  
  5. int main(){
  6.     int i,;
  7.     double n, m, o, p, q, r, s, t;
  8.     double scores[7];
  9.     scores[0] = n;
  10.     scores[1] = m;
  11.     scores[2] = o;
  12.     scores[3] = p;
  13.     scores[4] = q;
  14.     scores[5] = r; 
  15.     scores[6] = s;
  16.  
  17.     cout << "Enter seven numbers for me to store. I will then\n print each out, and their total\n";
  18.     cin >> n >> m >> o >> p >> q >> r >> s;
  19.     for(i = 0; i < 7; i++){
  20.           cout << scores[i] <<", ";
  21.  
  22.           }
  23.           cout << "\n The total sum =" << n + m + o + p + q + r + s;
  24.           system("PAUSE");
  25.           return 0;
  26.           }
  27.  
I tried initializing the array values with user inputted values on the same line of code, but it gave me a funky error.


Okay, so far the program takes everything in, and after entering the 7th number, it spits out Random letters, numbers... I've seen this happen when trying to do math with Letters, but I don't see how it's adding letters...they should have value.

Can I get a few tips ? Also, any tips for those for loops It says to write ?

I appreciate your time
Jul 21 '07 #1
6 1397
Hi, I'm pretty new here. I've started programming C++ on and off now for a while now, but never got anywhere with it. So I decided to stick with my book examples and see where it takes me.

Okay my goal is to write a program that prompts the user for 7 values, stores them in an array, and then prints each of them out, and their sum.
It also notes that I need two for loops to be included; one for collecting the inputs, and one for calculating the sum and printing the value.

This is what I have so far

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <math.h>
  3. using namespace std;
  4.  
  5. int main(){
  6.     int i,;
  7.     double n, m, o, p, q, r, s, t;
  8.     double scores[7];
  9.     scores[0] = n;
  10.     scores[1] = m;
  11.     scores[2] = o;
  12.     scores[3] = p;
  13.     scores[4] = q;
  14.     scores[5] = r; 
  15.     scores[6] = s;
  16.  
  17.     cout << "Enter seven numbers for me to store. I will then\n print each out, and their total\n";
  18.     cin >> n >> m >> o >> p >> q >> r >> s;
  19.     for(i = 0; i < 7; i++){
  20.           cout << scores[i] <<", ";
  21.  
  22.           }
  23.           cout << "\n The total sum =" << n + m + o + p + q + r + s;
  24.           system("PAUSE");
  25.           return 0;
  26.           }
  27.  
I tried initializing the array values with user inputted values on the same line of code, but it gave me a funky error.


Okay, so far the program takes everything in, and after entering the 7th number, it spits out Random letters, numbers... I've seen this happen when trying to do math with Letters, but I don't see how it's adding letters...they should have value.

Can I get a few tips ? Also, any tips for those for loops It says to write ?

I appreciate your time
When I compiled the program I received various errors. I think the problem is that you are not initializing your array to zeroes. For example, you're assigning score[0] the value n. What is the value of n? Also you are not storing any values in the array, that's where your other for loop would come in to play.
J
Jul 21 '07 #2
JosAH
11,448 Expert 8TB
Hi, I'm pretty new here. I've started programming C++ on and off now for a while now, but never got anywhere with it. So I decided to stick with my book examples and see where it takes me.

Okay my goal is to write a program that prompts the user for 7 values, stores them in an array, and then prints each of them out, and their sum.
It also notes that I need two for loops to be included; one for collecting the inputs, and one for calculating the sum and printing the value.

This is what I have so far

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <math.h>
  3. using namespace std;
  4.  
  5. int main(){
  6.     int i,;
  7.     double n, m, o, p, q, r, s, t;
  8.     double scores[7];
  9.     scores[0] = n;
  10.     scores[1] = m;
  11.     scores[2] = o;
  12.     scores[3] = p;
  13.     scores[4] = q;
  14.     scores[5] = r; 
  15.     scores[6] = s;
  16.  
  17.     cout << "Enter seven numbers for me to store. I will then\n print each out, and their total\n";
  18.     cin >> n >> m >> o >> p >> q >> r >> s;
  19.     for(i = 0; i < 7; i++){
  20.           cout << scores[i] <<", ";
  21.  
  22.           }
  23.           cout << "\n The total sum =" << n + m + o + p + q + r + s;
  24.           system("PAUSE");
  25.           return 0;
  26.           }
  27.  
I tried initializing the array values with user inputted values on the same line of code, but it gave me a funky error.


Okay, so far the program takes everything in, and after entering the 7th number, it spits out Random letters, numbers... I've seen this happen when trying to do math with Letters, but I don't see how it's adding letters...they should have value.

Can I get a few tips ? Also, any tips for those for loops It says to write ?

I appreciate your time
I think your understanding about loops and arrays is broken. Here's a little
trick:

1) get rid of that array;
2) rename your variables n, o, ... t to s0, s1, s2, ... s6;
3) rewrite your algorithm without using loops or arrays;
4) notice all that repeating code;
5) define an array 'double s[7];'
6) use variables s[0], s[1] ... s[6] instead of s0, s1, ... s6
7) get rid of the declarations of s0, s1, ... s6

At this stage you're using an array of seven doubles; you still should observe 4)
Now try to bring those loops back in; you did fairly well with them in your
original version of the code.

best of luck and

kind regards,

Jos
Jul 21 '07 #3
I figured it out !

Thanks to both of you, and the help you've given me!

Now my code looks like this:
Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. #include <math.h>
  4. using namespace std;
  5.  
  6. int main(){
  7.     int i, b;
  8.     double s0, s1, s2, s3, s4, s5, s6;
  9.     double s[7] = {s0, s1, s2, s3, s4, s5, s6};
  10.  
  11.     cout << "Enter numbers you want me to store, and print out, and then I'll add 'em all up!" ;
  12.     for(b = 0; b < 7; b++){
  13.           cin >> s[b];
  14.           }
  15.     for(i = 0; i < 7; i++) { 
  16.           cout << s[i] <<" ";
  17.           }
  18.     cout << "The total sum = " << s[0] + s[1] + s[2] + s[3] + s[4] + s[5] + s[6] << "!" << endl;
  19.           system("PAUSE");
  20.           return 0;
  21.           }
  22.  
  23.  
I got the 2 loops in for collecting, and printing the numbers input, but I can't seem to figure out how to squeeze in the total sum into the loop...

Would I have to declare and define a new function in order for it to be easier on the eyes ?
Jul 21 '07 #4
ilikepython
844 Expert 512MB
I figured it out !

Thanks to both of you, and the help you've given me!

Now my code looks like this:
Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. #include <math.h>
  4. using namespace std;
  5.  
  6. int main(){
  7.     int i, b;
  8.     double s0, s1, s2, s3, s4, s5, s6;
  9.     double s[7] = {s0, s1, s2, s3, s4, s5, s6};
  10.  
  11.     cout << "Enter numbers you want me to store, and print out, and then I'll add 'em all up!" ;
  12.     for(b = 0; b < 7; b++){
  13.           cin >> s[b];
  14.           }
  15.     for(i = 0; i < 7; i++) { 
  16.           cout << s[i] <<" ";
  17.           }
  18.     cout << "The total sum = " << s[0] + s[1] + s[2] + s[3] + s[4] + s[5] + s[6] << "!" << endl;
  19.           system("PAUSE");
  20.           return 0;
  21.           }
  22.  
  23.  
I got the 2 loops in for collecting, and printing the numbers input, but I can't seem to figure out how to squeeze in the total sum into the loop...

Would I have to declare and define a new function in order for it to be easier on the eyes ?
You dont need to declare all these extra double variables. Your array is already 7 double variables. For the sum, what you need is a sum variable and you adding the value to the sum each iteration of the for loop.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <math.h>
  3. using namespace std;
  4.  
  5. int main(){
  6.     int i, b;
  7.     double s[7] = {0};   // initialize all members to 0
  8.  
  9.     cout << "Enter numbers you want me to store, and print out, and then I'll add 'em all up!" ;
  10.     for(b = 0; b < 7; b++)
  11.     {
  12.         cin >> s[b];
  13.     }
  14.  
  15.     int sum = 0;
  16.     for(i = 0; i < 7; i++)
  17.     {
  18.         cout << s[i] <<" ";
  19.         sum += s[i];          // add value to sum
  20.     }
  21.     cout << "The total sum = " << sum << "!" << endl;
  22.     system("PAUSE");
  23.     return 0;
  24. }
Jul 21 '07 #5
Ahh, now I see.

I just realized that the book clearly defines the operator += a few chapters back (which I had forgotten about).

Thanks for the help, it worked out perfectly !
Jul 21 '07 #6
ilikepython
844 Expert 512MB
Ahh, now I see.

I just realized that the book clearly defines the operator += a few chapters back (which I had forgotten about).

Thanks for the help, it worked out perfectly !
You're welcome, I'm glad to help.
Jul 22 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: peter | last post by:
Hello all, Recently I've started to refactor my code ...(I'm using python 2.3.4) I tried to add extra functionality to old functions non-intrusively. When I used a construct, which involves...
14
by: Jim Hubbard | last post by:
Are you up to speed on the difficulties in using the 1.1 .Net framework? Not if you are unaware of the 1,596 issues listed at KBAlertz (http://www.kbalertz.com/technology_3.aspx). If you are...
19
by: Jim | last post by:
I have spent the past few weeks designing a database for my company. The problem is I have started running into what I believe are stack overflow problems. There are two tab controls on the form...
10
by: BBFrost | last post by:
We just recently moved one of our major c# apps from VS Net 2002 to VS Net 2003. At first things were looking ok, now problems are starting to appear. So far ... (1) ...
19
by: Dales | last post by:
I have a custom control that builds what we refer to as "Formlets" around some content in a page. These are basically content "wrapper" sections that are tables that have a colored header and...
2
by: Brian | last post by:
NOTE ALSO POSTED IN microsoft.public.dotnet.framework.aspnet.buildingcontrols I have solved most of my Server Control Collection property issues. I wrote an HTML page that describes all of the...
0
by: Peter R. Vermilye | last post by:
I am involved on a web application that is using a third party set of APIs for remote database access (middleware). I've been brought in because of my background in programming, thus I'm new to...
18
by: __PPS__ | last post by:
Hello, I'm a university student and I'm preparing for my final today. I'm reading course notes, I found completely strange piece of code. It makes me laugh, I think the teacher needs to prepare...
2
by: Mike | last post by:
Hi, I am new to C and having problems with the following program. Basically I am trying to read some files, loading data structures into memory for latter searching. I am trying to use structres...
9
by: =?Utf-8?B?SG93YXJkIFNtaXRo?= | last post by:
I am using VC++ 6.0 (with SP5 installed). When using WinXP this is with SP2 installed. I am developing an instrumentation system comprising a set of networked PCs connected using TCP/IP TCP links....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.