Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,398 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,109 people online right now. Registration is fast and FREE... Join Now!




Dealing with a thirteen digit integer

 
Reply to this topicStart new topic

Dealing with a thirteen digit integer

goldenllama
21 Oct, 2006 - 08:45 PM
Post #1

New D.I.C Head
*

Joined: 21 Oct, 2006
Posts: 8


My Contributions
Hi all, thanks for your help.

I have an assignment where I'm supposed to scan a file that contains a list of thirteen digit numbers. Each of these numbers represents votes cast for a candidate (for example, the number 0010000000000 would mean a vote cast for the third candidate). Originally I thought of pulling all these numbers and then using ifs to add votes to each candidate, but I get an error whenever I try to compile (I guess thirteen digits is too long?)

I'm looking for one of two solutions. Either, is there another way I could tally votes and have that 001000000000 number read? Or is there another way to store a thirteen digit number and just do it my way?

Thanks ahead of time!
User is offlineProfile CardPM
+Quote Post

UMTopSpinC7
RE: Dealing With A Thirteen Digit Integer
21 Oct, 2006 - 09:46 PM
Post #2

New D.I.C Head
*

Joined: 20 Oct, 2006
Posts: 47


My Contributions
I would store the number as a String or a char array if its really really long. It isn't that bad to do addition and stuff but it does take a little more work. An unsigned long long int (which is probably 64-bit) should be long enough for what you're doing. I'm pretty sure that would work if you're using GNU compiler.

QUOTE(goldenllama @ 21 Oct, 2006 - 09:45 PM) *

Hi all, thanks for your help.

I have an assignment where I'm supposed to scan a file that contains a list of thirteen digit numbers. Each of these numbers represents votes cast for a candidate (for example, the number 0010000000000 would mean a vote cast for the third candidate). Originally I thought of pulling all these numbers and then using ifs to add votes to each candidate, but I get an error whenever I try to compile (I guess thirteen digits is too long?)

I'm looking for one of two solutions. Either, is there another way I could tally votes and have that 001000000000 number read? Or is there another way to store a thirteen digit number and just do it my way?

Thanks ahead of time!

User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Dealing With A Thirteen Digit Integer
21 Oct, 2006 - 10:06 PM
Post #3

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,935



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
13 Binary Digits is totally okay if you store them in an integer data type.Max for int is 32 Bits for a 32-bit Compiler and 16 bits for a 16-bit compiler
But Don't store it as 000000....0010 but rather as an integer itself.
Use AND,OR,XOR Bitwise Operators to change and read whether the bit positions have been set.

It's the most accepted method for such implementations.
smile.gif
User is offlineProfile CardPM
+Quote Post

gregoryH
RE: Dealing With A Thirteen Digit Integer
21 Oct, 2006 - 10:31 PM
Post #4

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(UMTopSpinC7 @ 21 Oct, 2006 - 10:46 PM) *

I would store the number as a String or a char array if its really really long. It isn't that bad to do addition and stuff but it does take a little more work. An unsigned long long int (which is probably 64-bit) should be long enough for what you're doing. I'm pretty sure that would work if you're using GNU compiler.

QUOTE(goldenllama @ 21 Oct, 2006 - 09:45 PM) *

Hi all, thanks for your help.

I have an assignment where I'm supposed to scan a file that contains a list of thirteen digit numbers. Each of these numbers represents votes cast for a candidate (for example, the number 0010000000000 would mean a vote cast for the third candidate). Originally I thought of pulling all these numbers and then using ifs to add votes to each candidate, but I get an error whenever I try to compile (I guess thirteen digits is too long?)

I'm looking for one of two solutions. Either, is there another way I could tally votes and have that 001000000000 number read? Or is there another way to store a thirteen digit number and just do it my way?

Thanks ahead of time!



Have you considered a simple "hash" table that sums the 1 and zero's into an array the same size as the numbers? would work a treat
User is offlineProfile CardPM
+Quote Post

goldenllama
RE: Dealing With A Thirteen Digit Integer
22 Oct, 2006 - 10:40 AM
Post #5

New D.I.C Head
*

Joined: 21 Oct, 2006
Posts: 8


My Contributions
Thanks for the ideas. I played around with them a little and here's what I'm looking at now. It's obviously incomplete as I have more candidates to add, but I just did a short version to see if it works before I add everyone. I seem to be getting errors, though, and I can't figure out why.

CODE

#include <stdio.h>
#define FILENAME "election2005.dat"
main(void)
{
        int totvotes, dfrye, smcmillan, tknapp, ekolker, jsanders, sfrancis, mshelby, rrider, jledford, jbell, pshea, inva
lid, result = 0, power = 1, i;
        char vote[13];
        FILE *election2005;
        election2005 = fopen("election2005.dat", "r");
        if((election2005 = fopen(FILENAME, "r")) == NULL) {
                printf("Error opening the file\n");
                exit(1);
        }
        while(fscanf(election2005, "%s", vote) != EOF) {
                for(i = 0; i<13, i++)  // Line 58
                {
                        if(str[i] == '1')
                        {
                                binary[i] = 1;
                                result += power;
                        }
                power *= 2;
                }
                if (result = 4096) {  // Line 67
                        dfrye++;
                        totvotes++;
                }
                else if (result = 2048) {
                        smcmillan++;
                        totvotes++;
                }
                else if (result = 1024) {
                        tknapp++;
                        totvotes++;
                }
        }
                printf("Total votes: %d, for Donna %d, for smc %d for tknapp %d",totvotes, dfrye, smcmillan, tknapp);  // Line 80

        fclose(election2005);
        exit(0);
}


What am I missing?

This post has been edited by goldenllama: 22 Oct, 2006 - 01:57 PM
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Dealing With A Thirteen Digit Integer
22 Oct, 2006 - 11:14 AM
Post #6

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 45 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
Can you post the exact error message that you are getting?
User is offlineProfile CardPM
+Quote Post

goldenllama
RE: Dealing With A Thirteen Digit Integer
22 Oct, 2006 - 01:55 PM
Post #7

New D.I.C Head
*

Joined: 21 Oct, 2006
Posts: 8


My Contributions
code.c: In function `main':
code.c:58: error: parse error before ')' token
code.c: At top level:
code.c:67: error: parse error before "if"
code.c:80: error: parse error before string constant
code.c:80: warning: conflicting types for built-in function `printf'
code.c:80: warning: data definition has no type or storage class
code.c:82: warning: parameter names (without types) in function declaration
code.c:82: warning: data definition has no type or storage class
code.c:83: error: parse error before numeric constant
code.c:83: warning: data definition has no type or storage class

Because the line numbers are somewhat meaningless without context, I've gone ahead and added them into the previous post.
User is offlineProfile CardPM
+Quote Post

goldenllama
RE: Dealing With A Thirteen Digit Integer
22 Oct, 2006 - 08:46 PM
Post #8

New D.I.C Head
*

Joined: 21 Oct, 2006
Posts: 8


My Contributions
Caught the error. It was a stupid one, too. for(i = 0; i<13, i++) should have a semicolon, not a comma. Duh.

You guys have been amazing. Thanks so much for all the help!
User is offlineProfile CardPM
+Quote Post

NyeNye
RE: Dealing With A Thirteen Digit Integer
22 Oct, 2006 - 09:55 PM
Post #9

D.I.C Head
**

Joined: 24 Sep, 2006
Posts: 248


My Contributions
Nice you solve it..by your self...

we just help


but you did it...
User is offlineProfile CardPM
+Quote Post

gregoryH
RE: Dealing With A Thirteen Digit Integer
23 Oct, 2006 - 12:04 AM
Post #10

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(goldenllama @ 22 Oct, 2006 - 11:40 AM) *

Thanks for the ideas. I played around with them a little and here's what I'm looking at now. It's obviously incomplete as I have more candidates to add, but I just did a short version to see if it works before I add everyone. I seem to be getting errors, though, and I can't figure out why.


What am I missing?


CODE
if((election2005 = fopen(FILENAME, "r")) == NULL) {
                printf("Error opening the file\n");
                exit(1);
        }


You shouldn't be reopening the file...

try if ( election2005 == NULL )
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 02:59AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month