Jump to content

C++ Looping through a input Stream (Help)

Hello, Not sure if asking coding questions is still allowed here, if it's not let me know. I'm trying to figure out how to compare an input string variable from the user to a input stream string variable  (input stream contains 4 integers and 1 string). Since there is no order for the listing and what the user input I am having trouble when the user inputs incorrect data, otherwise if they put in a matching string it will output the information for that line.

 

The if condition where (popVoteState == stateName) will only work as long as the input is correct, however if the input is incorrect it doesn't prompt the user to reenter it or it would go line by line until it finds the input by prompting X amount of times until you reach it. 

 

How would I be able to search through the file for the user input to match, if it does match output the values associated for the string and if it doesn't match a state due to incorrect input (after searching through the file) it should re-prompt the user to input a correct state. 

    ifstream election12X, elecVote10X;

    election12X.open("elect12.csv");
    elecVote10X.open("ev10.csv");

    if (election12X.fail() && elecVote10X.fail())
    {
        cout << "File open failed" << endl;
        exit(1);
    }
    //Assert: "elect12.csv" and "ev10.csv" is open for input

    int numRegion;
    bool done = false;

    do
    {
        cout << "Enter the number of regions" << endl;
        cin >> numRegion;
    } while(numRegion < 0);

    for (numRegion; numRegion > 0; numRegion--)
    {
        cout << "Enter the state name" << endl;
        string stateName;
        cin >> stateName;

        while (election12X >> popVoteObama >> popVoteRomney >> popVoteOthers >> popVoteTotal >> popVoteState)
        {
            if (popVoteState == stateName)
            {
                cout << popVoteObama << endl;
            }
            if (election12X.eof() && popVoteState != stateName)
            {
      			cout << "Reeneter a state";
      			cin >> stateName;
            }
        }
        election12X.clear();
        election12X.seekg(ios::beg);
        //cout << endl;}
    }

    election12X.close();
    elecVote10X.close();

 

Desktop: Intel 4770k - 12GB Vengeance Pro 1866Mhz RAM - Asus Maximus VI Formula Mobo - Asus Strix 970 SLI - Cooler Master V850 PSU -  Nzxt Phantom 630 Case  - 1TB WD HDD - Samsung 840 Evo 250GB SSD - Nzxt Kraken X60 - 24" Asus VG248QE 1080p Monitor - Logitech G35 Headset -  G502 Proteus Core - Logitech G710+ Keyboard - Nzxt Hue - Windows 10

Link to comment
https://linustechtips.com/topic/669244-c-looping-through-a-input-stream-help/
Share on other sites

Link to post
Share on other sites

I think your issue lies in the fact that you are using two if statements. Wouldn't it be better to use else if for the second condition? I know C, not C++ but if I'm thinking right that might be the issue.

[Out-of-date] Want to learn how to make your own custom Windows 10 image?

 

Desktop: AMD R9 3900X | ASUS ROG Strix X570-F | Radeon RX 5700 XT | EVGA GTX 1080 SC | 32GB Trident Z Neo 3600MHz | 1TB 970 EVO | 256GB 840 EVO | 960GB Corsair Force LE | EVGA G2 850W | Phanteks P400S

Laptop: Intel M-5Y10c | Intel HD Graphics | 8GB RAM | 250GB Micron SSD | Asus UX305FA

Server 01: Intel Xeon D 1541 | ASRock Rack D1541D4I-2L2T | 32GB Hynix ECC DDR4 | 4x8TB Western Digital HDDs | 32TB Raw 16TB Usable

Server 02: Intel i7 7700K | Gigabye Z170N Gaming5 | 16GB Trident Z 3200MHz

Link to post
Share on other sites

1 minute ago, DeadEyePsycho said:

I think your issue lies in the fact that you are using two if statements. Wouldn't it be better to use else if for the second condition? I know C, not C++ but if I'm thinking right that might be the issue.

It does the same output for else if, this is the output with one correct input variable (as it searches through everything) and two incorrect variables (doesn't re-prompt)

 

Enter the number of regions
3
Enter the state name
NY
4485741
Enter the state name
aa
Enter the state name
aa

Desktop: Intel 4770k - 12GB Vengeance Pro 1866Mhz RAM - Asus Maximus VI Formula Mobo - Asus Strix 970 SLI - Cooler Master V850 PSU -  Nzxt Phantom 630 Case  - 1TB WD HDD - Samsung 840 Evo 250GB SSD - Nzxt Kraken X60 - 24" Asus VG248QE 1080p Monitor - Logitech G35 Headset -  G502 Proteus Core - Logitech G710+ Keyboard - Nzxt Hue - Windows 10

Link to post
Share on other sites

2 minutes ago, TheGosuStandard said:

It does the same output for else if, this is the output with one correct input variable (as it searches through everything) and two incorrect variables (doesn't re-prompt)

 

Enter the number of regions
3
Enter the state name
NY
4485741
Enter the state name
aa
Enter the state name
aa

What about an ordinary else statement?

[Out-of-date] Want to learn how to make your own custom Windows 10 image?

 

Desktop: AMD R9 3900X | ASUS ROG Strix X570-F | Radeon RX 5700 XT | EVGA GTX 1080 SC | 32GB Trident Z Neo 3600MHz | 1TB 970 EVO | 256GB 840 EVO | 960GB Corsair Force LE | EVGA G2 850W | Phanteks P400S

Laptop: Intel M-5Y10c | Intel HD Graphics | 8GB RAM | 250GB Micron SSD | Asus UX305FA

Server 01: Intel Xeon D 1541 | ASRock Rack D1541D4I-2L2T | 32GB Hynix ECC DDR4 | 4x8TB Western Digital HDDs | 32TB Raw 16TB Usable

Server 02: Intel i7 7700K | Gigabye Z170N Gaming5 | 16GB Trident Z 3200MHz

Link to post
Share on other sites

Have you checked if you read from file correctly? CSV format is strings delimited with comma character , if value contain special characters, it is closed in ".

When you read from stream using >> operator it will read until white space or character that is not matching variable, so after first read to popVoteObama it encounters comma character, and when you do second read by >> operator, it tries to read from that coma to integer, it will fail, and stream will be set bad flag.

 

Or your file is not csv format, can you give example of file?

 

I made example:

#include <fstream>
#include <iostream>

int main(){
	
	std::ifstream csv("file.csv");
	
	if(csv.good()){
		
		int a = 123;
		int b = 123;
		std::string c = "123";
		
		bool result;
		
		result = static_cast<bool>(csv >> a);
		std::cout << "Read: " << (result? "OK" : "FAILED") << " - [" << a << "] - streem state: " << (csv.good()? "GOOD" : "BAD") << std::endl;
		result = static_cast<bool>(csv >> b);
		std::cout << "Read: " << (result? "OK" : "FAILED") << " - [" << b << "] - streem state: " << (csv.good()? "GOOD" : "BAD") << std::endl;
		result = static_cast<bool>(csv >> c);
		std::cout << "Read: " << (result? "OK" : "FAILED") << " - [" << c << "] - streem state: " << (csv.good()? "GOOD" : "BAD") << std::endl;	
		
	} else {
		std::cout << "Unable to open file." << std::endl;
	}
	
	return 0;
}

for file, that contains:

1,2,Test

it will output:

Read: OK - [1] - streem state: GOOD
Read: FAILED - [0] - streem state: BAD
Read: FAILED - [123] - streem state: BAD

 

For file that contains:

1 2 Test

Output will be:

Read: OK - [1] - streem state: GOOD
Read: OK - [2] - streem state: GOOD
Read: OK - [Test] - streem state: BAD

The last bad is because good returns false because of EOF.

Link to post
Share on other sites

7 minutes ago, DeadEyePsycho said:

What about an ordinary else statement?

I believe I want a condition as a comparison, else would indicate that there is no other condition

 

4 minutes ago, Mr_KoKa said:

Have you checked if you read from file correctly? CSV format is strings delimited with comma character , if value contain special characters, it is closed in ".

When you read from stream using >> operator it will read until white space or character that is not matching variable, so after first read to popVoteObama it encounters comma character, and when you do second read by >> operator, it tries to read from that coma to integer, it will fail, and stream will be set bad flag.

 

Or your file is not csv format, can you give example of file?

It's an excel file there are spaces between each variable (doesn't show it in excel, in wordpad it does).  It reads from the file correctly if the only condition set is finding a match, however the issue occurs (or at least I am unsure how to set it up) for when the user input doesn't match. 

elect12.csv

Desktop: Intel 4770k - 12GB Vengeance Pro 1866Mhz RAM - Asus Maximus VI Formula Mobo - Asus Strix 970 SLI - Cooler Master V850 PSU -  Nzxt Phantom 630 Case  - 1TB WD HDD - Samsung 840 Evo 250GB SSD - Nzxt Kraken X60 - 24" Asus VG248QE 1080p Monitor - Logitech G35 Headset -  G502 Proteus Core - Logitech G710+ Keyboard - Nzxt Hue - Windows 10

Link to post
Share on other sites

10 minutes ago, TheGosuStandard said:

I believe I want a condition as a comparison, else would indicate that there is no other condition

 

The reason I suggested it was because if the condition for the if statement is not met, then you automatically presume the information entered is incorrect for a comparison since you have a predefined list of state names.

[Out-of-date] Want to learn how to make your own custom Windows 10 image?

 

Desktop: AMD R9 3900X | ASUS ROG Strix X570-F | Radeon RX 5700 XT | EVGA GTX 1080 SC | 32GB Trident Z Neo 3600MHz | 1TB 970 EVO | 256GB 840 EVO | 960GB Corsair Force LE | EVGA G2 850W | Phanteks P400S

Laptop: Intel M-5Y10c | Intel HD Graphics | 8GB RAM | 250GB Micron SSD | Asus UX305FA

Server 01: Intel Xeon D 1541 | ASRock Rack D1541D4I-2L2T | 32GB Hynix ECC DDR4 | 4x8TB Western Digital HDDs | 32TB Raw 16TB Usable

Server 02: Intel i7 7700K | Gigabye Z170N Gaming5 | 16GB Trident Z 3200MHz

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×