need help on ASCII conversion / end of file loops

Ok, I realize that I shouldn't be asking this, and probably will not get any responses, but it is worth a shot!
I have my first C++ program due in 4 hours and I need help, a lot of help!!!
I am supposed to write a program to read a series of integers using end-of-file loops, echo printing, and symbolic constants.
Unfortunately, my professor has not taught a lot thus far in class and I have not taught myself as I should in these situations. Does anybody have an example program (similar to mine) that they can share?? or any help on how to echo print, and/or convert ASCII to numbers???
Any help is appreciated, thank you...


OK this is my code so far:


* Programmer Corwin L Smith

// CSC 201 - Program # 1 "Calculate Grade"
// Use end-of-file loops and nested loops and convert ASCII to numbers
# include <iostream>
# include <fstream>

using namespace std;

int main ()
{
ifstream data1d; // input file
ofstream out1d; // output file
const int NUMHOMEWORK = 7; //
const int NUMPROGRAMS = 4; //
const float MAXPROGRAMS = 400.0; //
const char ASCIICONVERT = 0X0F; //

data1d.open ("dat1d.txt"); // name of input file
if (! dat1d)
{
cout<< "failure to open dat1d.txt" << endl;
system ("pause");
return 1; // error condition
}

out1d.open ("out1d.txt"); // output file created by program
if (! out1d)
{
cout<< "failure to open out1d.txt";
system ("pause");
return 1; // error condition
}









system ("PAUSE");

return 0;
}


can anyone tell me if it lloks like I am doing this right ???
I am new to coding in C++, but I plan on learning it well and doing a lot with it .. so please bear with me a little while I learn the basics ...

is that where I should declare my symbolic constants??
and i should open my data file and create an output file before i do the end of file loops right ???

thankx
Last edited on
I'd help cls20433, except that I don't hardly use any of the C++ iostream stuff. I hope somebody soon helps you though because by my reckoning you only have an hour and a half left!
yea my professor just emailed me and told me he changed the due date to monday , :), thank god!!
i hate to be so behind and ask this kind of thing of ppl, because I have every intention of learning the language and doing well in this class,
just been a rough start to the semester with moving, hurricane, no power, driving an hour each way for one class, yada, yada ... lol

thankx though,

and i def. still need help if anybody is willing!!
Will it be useful if i tell you in ASCII "0" is 0x30, "1" is 0x31, ... "9" is 0x39 ...?
Edit: I could have been helping you if you used stdio.h and not iostream.

1
2
3
4
5
int CharToInt(char Char)
{
 if(int(Char) >= 0x30 && int(Char) <= 0x39)
  return int(Char) - 0x30;
}


Code to convert a single character to a single number.

1
2
3
4
5
6
int StringToInt(char* String)
{
 int a;
 sscanf_s(String,strlen(String),"%d",&a);
 return a;
}


Code to convert a string to a number. Needs the inclusion of stdio.h.
Last edited on
I generally put things like this...

1
2
3
4
const int NUMHOMEWORK = 7; //
const int NUMPROGRAMS = 4; //
const float MAXPROGRAMS = 400.0; //
const char ASCIICONVERT = 0X0F; //  


...right after my includes at top. Also, do you know how to use code tags? A lot of folks won't even look at code if it isn't decently formatted with code tags. Maybe you could repost?

I'm like EssGeEich above. I don't use iostream. But you'll have to learn it unfortunately.
Here is a little program to get you started. You'll have to figure out how to convert it to use iostream. I made a text file with 8 grades in it like so named "Grades.txt"...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
87
65
91
100
105
91
75
83
*/

#include <cstdlib>
#include <cstdio>
#include <cstring>
const int MAX = 16;

int main()
{
 char szBuffer[MAX];
 int iGrade[8];
 FILE* fpIn=NULL;
 int iCtr=0;

 fpIn=fopen("Grades.txt","r");
 if(fpIn)
 {
    printf("iCtr\tiGrade[iCtr]\tszBuffer\n");
    printf("================================\n");
    while(true)
    {
      fgets(szBuffer,MAX-1,fpIn);
      if(!feof(fpIn))
      {
         iGrade[iCtr]=atoi(szBuffer);
         printf("%d\t%d\t\t%s",iCtr,iGrade[iCtr],szBuffer);
      }
      else
         break;
      iCtr++;
    }
    fclose(fpIn);
 }
 else
    printf("Couldn't Open Grades.txt\n");
 getchar();

 return 0;
}

/*
Program Output

iCtr    iGrade[iCtr]    szBuffer
================================
0       87              87
1       65              65
2       91              91
3       100             100
4       105             105
5       91              91
6       75              75
7       83              83
*/

Ok, sorry for taking so long to reply .. had a physics test and some other shit I had to take care of before delving back into the C++ work I have to do ... and yes that is helpful, but,
what do you mean by code-tag exactly???
I have the general structure of my loop down, just having trouble formatting the inside loops 0- which are suppose to control the flex. number of homeworks, and i have to make a statement to drop the lowest test grade too...
that is helpful though, I am going to go through your ex. and try and apply it to my program..
give me a couple hours and i will repost the latest on what I have so far ..
thankx again
Its hard to show how to use code tags as they are special formatting symbols. Here is from a recent webJose post where he somehow managed to get the idea across...

http://www.cplusplus.com/forum/windows/51511/

Properly formatted code makes it easy to understand what the code is doing in terms of such constructs as if statements, loops, etc. I'm pretty anal about it myself. I think a lot of coders are. I just want to see my code a certain way. But however you format it so that others can see levels of if/loop logic, any other coder can deal with it.
Topic archived. No new replies allowed.