Open Side Menu Go to the Top
Register
Programming homework and newbie help thread Programming homework and newbie help thread

12-03-2016 , 11:10 PM
This, by the way, is the simplest way to break down reading just a line at a time from a file, without worrying about parsing it or anything. I stole it from here
http://www.cplusplus.com/doc/tutorial/files/

Code:
  string line;
  ifstream myfile ("example.txt");
  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
      getline (myfile,line);
      cout << line << endl;
    }
    myfile.close();
  }
Programming homework and newbie help thread Quote
12-03-2016 , 11:41 PM
I did overload the >> operator. It works fine for the first line of text at least, but nothing else. I will take a deeper look at that code later and see if I did something weird that could be causing the problem. I just don't understand what could be causing the program to not even enter the while loop, or go through it only once.
Programming homework and newbie help thread Quote
12-03-2016 , 11:56 PM
Quote:
Originally Posted by LBloom
I did overload the >> operator. It works fine for the first line of text at least, but nothing else. I will take a deeper look at that code later and see if I did something weird that could be causing the problem. I just don't understand what could be causing the program to not even enter the while loop, or go through it only once.
My guess is that you're accidentally trashing the input stream somehow, like accidentally closing it or consuming the whole thing or something.
Programming homework and newbie help thread Quote
12-04-2016 , 12:02 AM
I will keep that in mind. Is there anything in particular I should look for that could cause that? As always, thanks for your help.
Programming homework and newbie help thread Quote
12-04-2016 , 12:17 AM
Quote:
Originally Posted by LBloom
I will keep that in mind. Is there anything in particular I should look for that could cause that? As always, thanks for your help.
I guess I'd have to see it to know. You could put some test output in your overload function to check the state of the stream at various places. Like look at mystream.good() maybe after you read. Or maybe output mystream.tellg() at various places - that will tell you your "position" in the input stream, in number of bytes. That may give a clue.
Programming homework and newbie help thread Quote
12-04-2016 , 12:22 AM
Also depending on how your overload function is written, there may be something wrong with the structure of your data file. Incorrect line ending might be one, or weird spacing or something. Maybe it's encountering an error in the spacing between Items in the data file, and setting an error bit, which makes future reading impossible. (That's what good() checks for, that none of the error bits are set)
Programming homework and newbie help thread Quote
12-04-2016 , 11:19 AM
Well, I solved the problem. What was causing the problem is I thought I could read something like 'Lbloom is confused' right from a data file into a char array like so:
inFile >> charArray;

But the spaces that some of the items in the file have make that not work. Thinking about it now it is obvious why I wouldn't be able to do that. So I changed it and now to fill the char array I have
inFile.getline(charArray, 31);
and everything works fine.

Thanks for helping me work through this last night. To me, one of the most challenging things about learning program is tracing the type of error that is occurring into what exactly is causing the error to happen.
Programming homework and newbie help thread Quote
12-04-2016 , 12:58 PM
You're exactly right. What you get from experience is not freedom from bugs, it's the ability to recognize them easier.
Programming homework and newbie help thread Quote
12-05-2016 , 01:34 AM
Had to write this little program for homework:

INPUT:

Code:
#include <stdio.h>
double Shrink(double value);
int Square(int value);
int Cube(int value);
int main ()

{

/* variable definition: */

   int intValue, menuSelect,Results;

   intValue = 1;

   // While a positive number

while (intValue > 0)

{  

     printf ("Enter a positive Integer\n: ");

     scanf("%d", &intValue);

   if (intValue > 0)

   {

     printf ("Enter 1 to calculate Square, 2 to Calculate Cube, 3 to divide by 2 \n: ");

     scanf("%d", &menuSelect);

     if (menuSelect == 1)

     {

       // Call the Square Function

       Results = Square(intValue);

       printf("Square of %d is %d\n",intValue,Results);

     }

     else if (menuSelect == 2)

     {

       // Call the Cube function

       Results = Cube(intValue);

     

     }
     
     //Shrink!
     
     else if (menuSelect == 3)
     
     {
     	
     	Results = Shrink(intValue);
     	
     	  printf("Half of %.2f is %.2f\n",(double)intValue,(double)Results);
     	
     	
     }

     else

       printf("Invalid menu item, only 1 or 2 is accepted\n");

     }    

   }    

return 0;

}

/* function returning the Square of a number */

int Square(int value)

{

   return value*value;

}

/* function returning the Cube of a number */

int Cube(int value)

{
   return value*value*value;

}

// shrinking!

double Shrink(double value)

{
	
	return (double)value/2;
}
PROBLEM:

Program does not output the decimal values and rounds to whole numbers.

For example:

If I input

Value = 377
Menu = 3
-1 (terminator)

it outputs 188.00 but should be 188.50

whats the deal?
Programming homework and newbie help thread Quote
12-05-2016 , 02:14 AM
You declared Results as an int, therefore when the result of Shrink is assigned to it, the value is cast to an int, with resulting loss of precision.
Programming homework and newbie help thread Quote
12-05-2016 , 02:23 AM
Quote:
Originally Posted by ChrisV
You declared Results as an int, therefore when the result of Shrink is assigned to it, the value is cast to an int, with resulting loss of precision.
How can I use the same variable for all of the functions?
Programming homework and newbie help thread Quote
12-05-2016 , 04:26 AM
You can make it a double instead, but theres no reason to have a middleman variable. You can call the functions straight out of the printf statements.
Programming homework and newbie help thread Quote
12-05-2016 , 02:44 PM
didnt want to start a thread, cuz im sure its probably been gone over before, but wanted to get some advice from those in the know.

I just transferred to a state school and they put me as a mathematics major instead of CS. I'm kind of contemplating just staying with math, as I'm really enjoying my math classes, and the math department has a equivalent of a cryptography minor that looks interesting. I know how to program at a level I think I could get a job with already. If my ultimate goal is to do something in the software engineering area, does the math major do anything for or against me?
Programming homework and newbie help thread Quote
12-05-2016 , 05:04 PM
They "put you" in a math major? How does that work? They were like, naw, **** you, you're going into math?

A solid foundation in math is an asset to a programmer, but there are a lot of CS courses you won't get the equivalent of that are much more on the theory side. Do you "need" these? I guess it kinda depends on what you want to end up doing.

I don't have a CS degree, I have an BSEE. That has helped me in some ways and hurt me in others. I did take all the requirements for a bachelors in CS though, while I was preparing for a MSCS that I didn't finish. So, I dunno.

It also kind of depends on by "do anything for or against me" you mean whether it'll hurt you in your abilities or hurt you in your ability to get hired. IME no one ever gave a **** if I had a CS degree or not. (However, I entered the job market at a time when they were hiring english majors for programming jobs, so....)
Programming homework and newbie help thread Quote
12-05-2016 , 09:21 PM
Quote:
Originally Posted by RustyBrooks
They "put you" in a math major? How does that work? They were like, naw, **** you, you're going into math?
It kind of seems like this is the case. I havent been able to get a hold of someone who can give me an answer. I had transferred in credits from like 7 different schools, so its a big cluster****.

Quote:
It also kind of depends on by "do anything for or against me" you mean whether it'll hurt you in your abilities or hurt you in your ability to get hired. IME no one ever gave a **** if I had a CS degree or not. (However, I entered the job market at a time when they were hiring english majors for programming jobs, so....)
pretty much yeah all I care about is how it impacts my hire ability. I'm kind of sour on the relevance of college. I can learn all this **** on my own and in way less time, so I'm not worried about missing out on some CS "theory" class cuz I can just learn that whenever. I'm pretty much just getting a degree because I failed at it when I was younger. I just don't want to **** myself by deciding "screw it, I dont want to deal with this bull****, math is cool, ill just go ahead and major in that".
Programming homework and newbie help thread Quote
12-05-2016 , 09:35 PM
I was going to give you a lecture about the pretty thick math classes you have to wade through in CS and how likely you are to actually do them on your own, but then I went and looked at a modern stanford class schedule and a lot of those either aren't present, or are named something other than I'd expect or taught differently.

A lot of classes that sounded really cool actually sucked balls, because it turns out you can't really teach that much aiming at the center of the bell curve a few hours a week, especially since when I was taking CS it was not a given that everyone could program. Most couldn't program at all to start, and there was NOT a huge programming requirement. It was largely theoretical.

So, like, classes like computer vision or AI really to get interesting you have to be able to program, the math is pretty hairy past toy examples so you need to start experimenting. So I found those classes just freakin useless. Maybe the profs sucked too, I dunno.
Programming homework and newbie help thread Quote
12-05-2016 , 09:39 PM
My last question of the semester....

In C++, I am trying to output the contents of a vector to a file, line by line. My code looks like this:
Code:
outFile.open("Assignment33Data.txt");
			for (position = storeStock.begin(); position != storeStock.end(); position++)
			{
				outFile << *position;
			}
What this does is output the data I want, but to the console. It does create the file, but it's blank. What could be going on here? I declared ofstream outFile earlier in the program.
Programming homework and newbie help thread Quote
12-05-2016 , 09:48 PM
Again,
outfile << *position
is not a builtin function - you therefore had to make a operator overload function for the stream output operator. The error is probably in that function. My guess is that you're using cout or cerr instead of the passed in ostream.
Programming homework and newbie help thread Quote
12-05-2016 , 09:49 PM
im a 1st semester CS student.

fwiw, alot of the job posts that I see in the software eng/dev type area commonly say things like "CS, engineering, mathematics or related degree."

I think you are probably fine. You may even find that a degree in mathematics may be more beneficial then CS in some arenas,

My guess is that you probably have such a hodge podge of credits they are trying to find the most optimal degree for you to complete in their institution. that appears to me math.
Programming homework and newbie help thread Quote
12-05-2016 , 10:26 PM
Quote:
Originally Posted by RustyBrooks
Again,
outfile << *position
is not a builtin function - you therefore had to make a operator overload function for the stream output operator. The error is probably in that function. My guess is that you're using cout or cerr instead of the passed in ostream.
Your guess is absolutely correct. I was just coming back to post a never mind. Especially annoying because I spent a bunch of time on exactly the same issue a month or so ago. Well, thanks for all the help this semester!
Programming homework and newbie help thread Quote
12-12-2016 , 01:01 PM
nvm im a ******.

Last edited by jmakin; 12-12-2016 at 01:16 PM.
Programming homework and newbie help thread Quote
12-12-2016 , 01:11 PM
Reverse i-- and i>=5. The order is supposed to be (init, condition, increment).
Programming homework and newbie help thread Quote
12-12-2016 , 01:19 PM
yea, lol, too much haskell. the set builder in haskell is increment, bound, condition. was hoping no one saw that. The weird thing was it was still running and working. <3 C++
Programming homework and newbie help thread Quote
12-13-2016 , 06:10 AM
Trying to add the sum of all years data using loop.

HAAAALP I dont know what im doing.

Code:
#define NUMMONTHS 12
#define NUMYEARS 5
#include <stdio.h>
// function prototypes
void inputdata();
void printdata();
// Global variables
// These are available to all functions
float Raindata[NUMYEARS][NUMMONTHS];
char years[NUMYEARS][5] = {"2011","2012","2013","2014","2015"};
char months[NUMMONTHS][12]
={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
int main ()
{
	

	
 char enterData = 'y';
 printf("Do you want to input Precipatation data? (y for yes)\n");
 scanf("%c",&enterData);
 if (enterData == 'y') {
 // Call Function to Input data
 inputdata();

 // Call Function to display data
 printdata();
 }
 else {
 printf("No data was input at this time\n");
 }
 printf("Please try the Precipitation program again. \n");
 return 0;
}
// function to inputdata
void inputdata() {
 /* variable definition: */
 float Rain=1.0;
 // Input Data
 for (int year=0;year < NUMYEARS; year++) {
 for (int month=0; month< NUMMONTHS; month++) {
 printf("Enter rain for %d, %d:\n", year+1, month+1);
 scanf("%f",&Rain);
 Raindata[year][month]=Rain; 

 }
 }
}
// Function to printdata
void printdata(){
// Print data
 printf ("year\t month\t rain\n");
 for (int year=0;year < NUMYEARS; year++) {
 for (int month=0; month< NUMMONTHS; month++) {
 printf("%s\t %s\t %5.2f\n",
years[year],months[month],Raindata[year][month]);
 }
 }
}

// adding sum
void sumit(){
	

int i, sum;
 sum = 0;
   for (i = 0; i < NUMYEARS; i++)
      sum = sum + NUMYEARS[5];
      
      printf("The sum of all years is %d\n",sum);
      
      
      }

output:

Compilation error:

prog.c: In function 'sumit':
prog.c:67:27: error: subscripted value is neither array nor pointer nor vector
sum = sum + NUMYEARS[5];
^

Last edited by de4df1sh; 12-13-2016 at 06:27 AM.
Programming homework and newbie help thread Quote
12-13-2016 , 06:41 AM
NUMYEARS is just an integer which is equal to 5, so of course you can't do NUMYEARS[5], you can only do that to arrays pointers and vectors, as the compiler tells you.

It's not clear what you want to do in the sumit function, are you trying to compute the sum 2011 + 2012 + 2013 + 2014+ 2015?
Programming homework and newbie help thread Quote

      
m