C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 8th, 2011, 09:49 AM
willy155 willy155 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2010
Posts: 51 willy155 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 24 m 24 sec
Reputation Power: 4
Reading string of numbers from .txt file and calculating two values from the numbers

I am having troubles sorting out what should be a very simple piece of code. It takes in a string of numbers (which represent votes out of 5 for something) from a .txt file and should then work out the total number of votes, and the average of those votes. It is getting the total number of votes right but for some reason it is messing up on the average...the code is as follows:

Code:
public Form1()
{
            InitializeComponent();
            string votes = System.IO.File.ReadAllText(@"..\..\votes.txt");
            int votesSum = 0;
            int totalVotes = 0;
            foreach (char b in votes)
            {
                totalVotes++;
            }

            foreach (char c in votes)
            {
                int d = Convert.ToInt32(c);
                votesSum = votesSum + d;
            
            }
            Console.Write(votesSum);
            double avg = votesSum / totalVotes;
            double average = Math.Round(avg);
            lblTotal.Text = totalVotes.ToString();
            lblAvg.Text = average.ToString();
}


the current contents of 'votes.txt' are:

134253413243524

I make the total votes to be 15, the sum should be 46 and the average should be 3.06(recurring)
I've tried inserting a breakpoint in at the start of the second foreach loop but for some reason (on the first iteration) its saying the following:

d = 49
c = 49 '1'

when i step through the whole code it says

votesSum = 766
totalVotes = 15
avg = 51
average = 51


can someone work out why my code is not doing what it should?

Reply With Quote
  #2  
Old May 8th, 2011, 12:54 PM
David_B David_B is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 67 David_B User rank is Sergeant (500 - 2000 Reputation Level)David_B User rank is Sergeant (500 - 2000 Reputation Level)David_B User rank is Sergeant (500 - 2000 Reputation Level)David_B User rank is Sergeant (500 - 2000 Reputation Level)David_B User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 1 h 30 m 9 sec
Reputation Power: 23
The character "1" has an ASCII value of 49, so it looks like it is not being converted into an integer. The variable d is simply getting the ASCII equivalents of all the characters in the input string. Check that conversion function.
__________________
Regards,


David
Quadratic Equation Solver . . . and more.

Reply With Quote
  #3  
Old May 8th, 2011, 08:25 PM
willy155 willy155 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2010
Posts: 51 willy155 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 24 m 24 sec
Reputation Power: 4
sorted now...you were right, it was to do with the conversion. all i had to change the offending line to was:

Code:
int d = Convert.ToInt32(c.ToString());

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Reading string of numbers from .txt file and calculating two values from the numbers

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap