Click here to Skip to main content
15,881,833 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include <iostream>
#include <string>
#include <fstream>

using namespace std;
int main()
{
string truck_Num;
string modelType ;
ifstream fin;

fin.open("Trucks.txt");

//Write a while loop that reads the records from file.
while(!(fin.eof()))
{
    fin>>truck_Num;
    cout << "Truck number " << truck_Num <<" is a/an " << modelType << " model." <<endl;
fin>>truck_Num;
fin>>modelType;
   }
fin.close();

return 0;
}


Printing::

Truck number Truck is a/an  model.
Truck number Truck is a/an International model.
Truck number Truck is a/an Sterling model.
Truck number Truck is a/an Volvo model.
Truck number Truck is a/an Freightliner model.
Truck number Truck is a/an International model.
Truck number Truck is a/an Freightliner model.
Truck number 132 is a/an Sterling model.


What I have tried:

I have tried multiple different ways.
Posted
Updated 13-Dec-20 8:08am
v2

Look at your code (I'll indent it properly so it's more readable):
//Write a while loop that reads the records from file.
C++
while(!(fin.eof()))
   {
   fin>>truck_Num;
   cout << "Truck number " << truck_Num <<" is a/an " << modelType << " model." <<endl;
   fin>>truck_Num;
   fin>>modelType;
   }
You read the truck number, and print it - and the model which you haven't read.
Then you read the truck number again.
Then you read the model.
Then it's back round the loop: read the truck again (and discard the one you just read) and print it, and the previous model number.

Does any of that sound sensible to you?
Read the truck number, and then the model.
Then print them, and go back round the loop.

This assumes that your text file contains valid information and no blank lines - and we have no access to that!

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Comments
cksp0991 13-Dec-20 12:40pm    
The debugger doesn't show any errors or bring up anything wrong with the code. And I watched the videos. It comes up with nothing.
OriginalGriff 13-Dec-20 12:43pm    
"The debugger doesn't show any errors or bring up anything wrong with the code."
That's not the debugger. That's called a compiler.
The debugger lets you look at and control your code while it's running, examining (and changing) variables, single stepping to execute one instruction at a time, that sort of thing.
cksp0991 13-Dec-20 12:49pm    
I know the difference. When I set up the new project like it states in the video to do in Codeblocks for it's debug, it shows nothing. It says this:

Active debugger config: GDB/CDB debugger:Default
Building to ensure sources are up-to-date
Selecting target:
Debug
Adding source dir: C:\Users\cksp0\OneDrive\Documents\CODEBlocks\DEBUG\
Adding source dir: C:\Users\cksp0\OneDrive\Documents\CODEBlocks\
Adding file: C:\Users\cksp0\OneDrive\Documents\CODEBlocks\DEBUG\bin\Debug\DEBUG.exe
Changing directory to: C:/Users/cksp0/OneDrive/Documents/CODEBlocks/DEBUG/.
Set variable: PATH=.;C:\Program Files\CodeBlocks\MinGW\bin;C:\Program Files\CodeBlocks\MinGW;C:\Windows\System32;C:\Windows;C:\Windows\System32\wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Users\cksp0\AppData\Local\Microsoft\WindowsApps
Starting debugger: C:\Program Files\CodeBlocks\MINGW\bin\gdb.exe -nx -fullname -quiet -args C:/Users/cksp0/OneDrive/Documents/CODEBlocks/DEBUG/bin/Debug/DEBUG.exe
done
Setting breakpoints
Debugger name and version: GNU gdb (GDB) 8.1
Child process PID: 12768
At C:\Users\cksp0\OneDrive\Documents\CODEBlocks\NEWFILE.cpp:19
Continuing...
[Inferior 1 (process 12768) exited normally]
Debugger finished with status 0
cksp0991 13-Dec-20 13:02pm    
When I change it to this::

fin.open("Trucks.txt");

fin>>truckNum;

//Write a while loop that reads the records from file.
while(!(fin.eof()))
{
fin>>modelType;
cout << "Truck number " << truckNum <<" is a " << modelType << " model." <<endl;
fin>>truckNum;


}
fin.close();

return 0;
}


It is repeating "Truck number is a model." continuously.
First, you should show us your input. Without it we have to guess at what might be going on.

Second, you have your output in the middle of reading the text file, so on the first loop truck_num and modelType are empty. That might make sense depending on the format of your input, but it seems unlikely.

Third, it looks like you might have space-separated data records. This might be an issue if the modelType has a space, say International Harvester, It might be better use a non-space character as the field delimiter

Fourth, You have variables truck_num and modelType. For consistency you should choose a style and stick with it. This is just a style issue, and won't affect the performance of the program in any way.
 
Share this answer
 
Comments
cksp0991 13-Dec-20 12:05pm    
This is what I have for the text file to read from.

172
International
195
Sterling
207
Volvo
218
Freightliner
219
International
96
Freightliner
132
Sterling
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Your loop should look more like this :
C++
while( ! fin.eof() )
{
    fin >> truck_Num;
    fin >> modelType;
    cout << "Truck number " << truck_Num <<" is model " << modelType << " model." <<endl;
}
you should read the data before you try to output it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900