Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Bigred101

macrumors newbie
Original poster
Mar 13, 2014
2
0
a list of the 50 state abbreviations as well as the population for each of the states. You are to write a C++ program that will read this file and place the abbreviations in a string array and the population into an int array. Note that there will not be more than 55 values in each array. The population value is expressed in 1,000’s of people.

Once the values are read into the appropriate arrays, you are to set up the code that allows a user to find the population for a particular state. You should prompt the user for a state to search for. Search the state array (containing the state abbreviation) using the sequential search routine from our text. Once a state’s position in the array is found, then the value in the population array will be the answer. If a person enters a state abbreviation that is not in the array, then an appropriate error message should be printed. Allow a user to enter different states as desired (hint: using a while loop or a do-while loop).


Nothing is working help!!
 
Tell us more about the "nothing" ... What do you have already ? What is/are the problems ?

Oh, and you might could make your own thread instead of reanimate a four year old one, but fine too.
(Update: Seems some helping mod did that)
 
Last edited:
Nothing is working help!!

It's not possible to find the reason why "nothing is working" by reading your homework assignment.

I think you are probably wrong though, and some parts actually are working. Just start to scale back on features on the assignment, comment out code etc. to find a simpler version that does work. Then add in features and compile often, you will then be able to detect exactly what is not working and can also formulate a meaningful question about it so that someone can help you.
 
Code:
#include <iostream>  
#include <fstream>  
#include <string>  
   
using namespace std;  
    

int main()  

 {  
	string str[55];  
	ifstream myfile("StatePop.txt");  
	int a = 0;  
	int b = 0;  
	if(!myfile)  

     {  
		cout<<"Error opening output file"<<endl;  
		system("pause");  
		return -1;  
     }  
	 char nextToken;  
	 char  array[55];  
	 while (myfile >> nextState) {  
		
		array[a] = nextState;  
		 
        a++;  
    }  

for (int i =0; i < a; i++)  
	{  
	cout << array[i] << endl;   


}  

	system("pause"); 
	return 0;  

}
My file isn't working....I'm really lost
 
Last edited by a moderator:
Can you put comments into your source code with reference to single parts from the assignment ? It would help us (and you) to better understand what your intention for each source code line is. E.g. Your array definitions and loops. There will be the learning coming from.
How looks the text file ? Maybe just the first 10 lines ?
 
My file isn't working....I'm really lost

system("pause") are Windows specific IDE hacks, not necessary on a Mac and will not work. Your variable nextToken, should probably be nextState? Also make sure that the file StatePop.txt actually exist, and is placed in the same directory you are running the program in. That's a start.
 
system("pause") are Windows specific IDE hacks, not necessary on a Mac and will not work

All you have to do to ditch this line is run the code in a terminal or in an IDE that doesn't automatically clear/close the console when the program finishes execution. We used a godawful IDE in my freshman engineering classes called Bloodshed Dev C++ - it required that line in the code if you wanted to run from inside the IDE because it would automatically closer the terminal window otherwise.

Although that's really just not necessary in this case, because the instructions tell you to wrap the program in a while loop, which means your program won't terminate.

bigred, just dumping your code on us is still not helpful. Which particular line isn't working for you? What input is it receiving, what output are you expecting, and what output is it actually producing? That code you posted doesn't even look like it'll compile - you also use a variable nextState (twice, in fact) but you never declare it. You also have a char variable nextToken which you declare but then never use (which should produce a warning from your compiler, but not an error - it should compile fine with that even though it doesn't make sense to declare but not use a variable like that.)
 
All you have to do to ditch this line is run the code in a terminal or in an IDE that doesn't automatically clear/close the console when the program finishes execution. We used a godawful IDE in my freshman engineering classes called Bloodshed Dev C++ - it required that line in the code if you wanted to run from inside the IDE because it would automatically closer the terminal window otherwise.

That's exactly what I was saying.

Although that's really just not necessary in this case, because the instructions tell you to wrap the program in a while loop, which means your program won't terminate.

The program prints the error message and the contents of 'array' to stdout, the terminal window will disappear before you are able to read it if this is run on Bloodshed for example.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.