Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › C++ - Single-line input validation
New Posts  All Forums:Forum Nav:

C++ - Single-line input validation

post #1 of 8
Thread Starter 
Hey guys. I've recently started uni again and I'm a bit rusty at C++ input - was wondering if someone could shed some light on this issue for me!

Basically, I need to ask the user to input a set amount of 'laboratory marks'. Now, this could be 5, or 6, or any number (I don't have a problem sorting loops for this).

The issue is that they have to enter numbers on the same line. For example five lab results would be something like:

Code:
Enter 5 laboratory marks: -1 0 0 0.5 1
Now, the only acceptable lab marks are -1, 0, 0.5, 1. -1 indicates a missed lab.

The program needs to, after the user has input, for example:

Code:
Enter 5 laboratory marks: -1 2 3 1 0.5
The program then needs to say something like:

Code:
Please enter lab mark 2 again: ...
Please enter lab mark 3 again: ...
I have no problems getting the data into an array, I just need to work out how to validate it.

I have tried using cin >> to a cstring array (we haven't learned about strings yet therefore can't use them, much to my disgust) then validating the array to no avail. Maybe it's just something I'm overlooking, I don't know.

Here's the code I have at the moment (it's rusty in some places but I'll paste it anyway):

Code:
// Laboratory marks input
cout << "Enter " << completedLabs << " laboratory marks (-1 indicates missed lab):" << endl;

char tempMarks[4][NUM_MARKS];
int attempts[NUM_MARKS] = {1};// Temp array for error checking
int failedAttempts = 0;

// Loop through cin's, checking input
for (int i = 0; i < completedLabs; i++) {
cin >> tempMarks[i];

if (!tempMarks[i] == '-1' || !tempMarks[i] == '0' || !tempMarks[i] == '0.5' || !tempMarks[i] == '1') {
failedAttempts++;
attempts[i] = 0;// Indicates that the check failed for that element
}
}

for (int i = 0; i < completedLabs; i++) {
cout << tempMarks[i] << ", ";
}

if (failedAttempts > 0) {
cerr << "ERROR: " << failedAttempts << " laboratory marks were entered incorrectly." << endl;
}

cin.ignore(256, '\
');
cin.clear();

bool goodInput;

for (int i = 0; i < completedLabs; i++) {
goodInput = false;

if (attempts[i] == 0) {
while (goodInput == false) {
cout << "Enter lab " << i+1 << " mark again: ";
cin >> tempMarks[i];

if (!tempMarks[i] == '-1' || !tempMarks[i] == '0' || !tempMarks[i] == '0.5' || !tempMarks[i] == '1')
goodInput = false;
else
goodInput = true;
}
}
}
A push in the right direction would be greatly appreciated!
Project 4
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 Lynnfield @ 4.4 GHz, 1.41v EVGA P55 SLi (132-LF-E655-KR) - A72 BIOS EVGA GeForce GTX 680 4GB G.Skill Ripjaws @ 9-9-9-24, 1.51V 
Hard DriveOptical DriveOSMonitor
80GB Intel X25-M SSD + 500GB WD + 2TB Samsung F3 Samsung Super Writemaster DVD+-R Windows 7 Ultimate Edition Samsung SyncMaster 2433 24" 
KeyboardPowerCaseMouse
Logitech G15 Corsair TX750 750W Corsair Obsidian 800D + Scythe Kaze Master Ace Razer Deathadder Respawn - Black 
Mouse Pad
Steelseries QcK Medium - Black 
  hide details  
Reply
Project 4
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 Lynnfield @ 4.4 GHz, 1.41v EVGA P55 SLi (132-LF-E655-KR) - A72 BIOS EVGA GeForce GTX 680 4GB G.Skill Ripjaws @ 9-9-9-24, 1.51V 
Hard DriveOptical DriveOSMonitor
80GB Intel X25-M SSD + 500GB WD + 2TB Samsung F3 Samsung Super Writemaster DVD+-R Windows 7 Ultimate Edition Samsung SyncMaster 2433 24" 
KeyboardPowerCaseMouse
Logitech G15 Corsair TX750 750W Corsair Obsidian 800D + Scythe Kaze Master Ace Razer Deathadder Respawn - Black 
Mouse Pad
Steelseries QcK Medium - Black 
  hide details  
Reply
post #2 of 8
Why not use an integer array? Your data could be handled much more simply that way.
WC ftw!
(13 items)
 
  
CPUMotherboardGraphicsRAM
C2D E7200 @ 3.51ghz 1.17v Lapped Gigabyte EP35-DS3L, HR-05 on nb Palit 8800gt 8gb G.Skill @ 1000mhz 2.1v 
Hard DriveOptical DriveOSMonitor
2x7200.12 500gb RAID, 2xWD5000AAKS Asus DRW-1814BLT SATA CLFSx64, W7x64 SCEPTRE 22" 1680x1050 
KeyboardPowerCaseMouse
Logitech Media Corsair HX520 Modular Antec P182 Gunmetal Logitech G7 Laser Cordless 
Mouse Pad
none 
  hide details  
Reply
WC ftw!
(13 items)
 
  
CPUMotherboardGraphicsRAM
C2D E7200 @ 3.51ghz 1.17v Lapped Gigabyte EP35-DS3L, HR-05 on nb Palit 8800gt 8gb G.Skill @ 1000mhz 2.1v 
Hard DriveOptical DriveOSMonitor
2x7200.12 500gb RAID, 2xWD5000AAKS Asus DRW-1814BLT SATA CLFSx64, W7x64 SCEPTRE 22" 1680x1050 
KeyboardPowerCaseMouse
Logitech Media Corsair HX520 Modular Antec P182 Gunmetal Logitech G7 Laser Cordless 
Mouse Pad
none 
  hide details  
Reply
post #3 of 8
Thread Starter 
Quote:
Originally Posted by Gauvenator View Post
Why not use an integer array? Your data could be handled much more simply that way.
And how would it handle '0.5'?
Project 4
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 Lynnfield @ 4.4 GHz, 1.41v EVGA P55 SLi (132-LF-E655-KR) - A72 BIOS EVGA GeForce GTX 680 4GB G.Skill Ripjaws @ 9-9-9-24, 1.51V 
Hard DriveOptical DriveOSMonitor
80GB Intel X25-M SSD + 500GB WD + 2TB Samsung F3 Samsung Super Writemaster DVD+-R Windows 7 Ultimate Edition Samsung SyncMaster 2433 24" 
KeyboardPowerCaseMouse
Logitech G15 Corsair TX750 750W Corsair Obsidian 800D + Scythe Kaze Master Ace Razer Deathadder Respawn - Black 
Mouse Pad
Steelseries QcK Medium - Black 
  hide details  
Reply
Project 4
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 Lynnfield @ 4.4 GHz, 1.41v EVGA P55 SLi (132-LF-E655-KR) - A72 BIOS EVGA GeForce GTX 680 4GB G.Skill Ripjaws @ 9-9-9-24, 1.51V 
Hard DriveOptical DriveOSMonitor
80GB Intel X25-M SSD + 500GB WD + 2TB Samsung F3 Samsung Super Writemaster DVD+-R Windows 7 Ultimate Edition Samsung SyncMaster 2433 24" 
KeyboardPowerCaseMouse
Logitech G15 Corsair TX750 750W Corsair Obsidian 800D + Scythe Kaze Master Ace Razer Deathadder Respawn - Black 
Mouse Pad
Steelseries QcK Medium - Black 
  hide details  
Reply
post #4 of 8
Quote:
Originally Posted by jNSK;14431451 
And how would it handle '0.5'?

whoops. try float or double array then.
WC ftw!
(13 items)
 
  
CPUMotherboardGraphicsRAM
C2D E7200 @ 3.51ghz 1.17v Lapped Gigabyte EP35-DS3L, HR-05 on nb Palit 8800gt 8gb G.Skill @ 1000mhz 2.1v 
Hard DriveOptical DriveOSMonitor
2x7200.12 500gb RAID, 2xWD5000AAKS Asus DRW-1814BLT SATA CLFSx64, W7x64 SCEPTRE 22" 1680x1050 
KeyboardPowerCaseMouse
Logitech Media Corsair HX520 Modular Antec P182 Gunmetal Logitech G7 Laser Cordless 
Mouse Pad
none 
  hide details  
Reply
WC ftw!
(13 items)
 
  
CPUMotherboardGraphicsRAM
C2D E7200 @ 3.51ghz 1.17v Lapped Gigabyte EP35-DS3L, HR-05 on nb Palit 8800gt 8gb G.Skill @ 1000mhz 2.1v 
Hard DriveOptical DriveOSMonitor
2x7200.12 500gb RAID, 2xWD5000AAKS Asus DRW-1814BLT SATA CLFSx64, W7x64 SCEPTRE 22" 1680x1050 
KeyboardPowerCaseMouse
Logitech Media Corsair HX520 Modular Antec P182 Gunmetal Logitech G7 Laser Cordless 
Mouse Pad
none 
  hide details  
Reply
post #5 of 8
Thread Starter 
Quote:
Originally Posted by Gauvenator View Post
whoops. try float or double array then.
I have. You're missing the point entirely. I only have an issue with the validation, not storing the numbers themselves.
Project 4
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 Lynnfield @ 4.4 GHz, 1.41v EVGA P55 SLi (132-LF-E655-KR) - A72 BIOS EVGA GeForce GTX 680 4GB G.Skill Ripjaws @ 9-9-9-24, 1.51V 
Hard DriveOptical DriveOSMonitor
80GB Intel X25-M SSD + 500GB WD + 2TB Samsung F3 Samsung Super Writemaster DVD+-R Windows 7 Ultimate Edition Samsung SyncMaster 2433 24" 
KeyboardPowerCaseMouse
Logitech G15 Corsair TX750 750W Corsair Obsidian 800D + Scythe Kaze Master Ace Razer Deathadder Respawn - Black 
Mouse Pad
Steelseries QcK Medium - Black 
  hide details  
Reply
Project 4
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 Lynnfield @ 4.4 GHz, 1.41v EVGA P55 SLi (132-LF-E655-KR) - A72 BIOS EVGA GeForce GTX 680 4GB G.Skill Ripjaws @ 9-9-9-24, 1.51V 
Hard DriveOptical DriveOSMonitor
80GB Intel X25-M SSD + 500GB WD + 2TB Samsung F3 Samsung Super Writemaster DVD+-R Windows 7 Ultimate Edition Samsung SyncMaster 2433 24" 
KeyboardPowerCaseMouse
Logitech G15 Corsair TX750 750W Corsair Obsidian 800D + Scythe Kaze Master Ace Razer Deathadder Respawn - Black 
Mouse Pad
Steelseries QcK Medium - Black 
  hide details  
Reply
post #6 of 8
Quote:
Originally Posted by jNSK;14431531 
I have. You're missing the point entirely. I only have an issue with the validation, not storing the numbers themselves.

Ok, well, if you are sticking with your array of pointers, you can't compare pointers to string literals to get the effect you want.
Code:
if(tempmarks[i] == "-1")
(note the double quotes, you are using single quotes which means a character literal)
The above code will not give you the result you desire
You are looking for something like
Code:
strcmp(tempmarks[i],"-1")
This is a great website for documentation on the standard functions:
http://www.cplusplus.com/reference/

hopefully this helps
WC ftw!
(13 items)
 
  
CPUMotherboardGraphicsRAM
C2D E7200 @ 3.51ghz 1.17v Lapped Gigabyte EP35-DS3L, HR-05 on nb Palit 8800gt 8gb G.Skill @ 1000mhz 2.1v 
Hard DriveOptical DriveOSMonitor
2x7200.12 500gb RAID, 2xWD5000AAKS Asus DRW-1814BLT SATA CLFSx64, W7x64 SCEPTRE 22" 1680x1050 
KeyboardPowerCaseMouse
Logitech Media Corsair HX520 Modular Antec P182 Gunmetal Logitech G7 Laser Cordless 
Mouse Pad
none 
  hide details  
Reply
WC ftw!
(13 items)
 
  
CPUMotherboardGraphicsRAM
C2D E7200 @ 3.51ghz 1.17v Lapped Gigabyte EP35-DS3L, HR-05 on nb Palit 8800gt 8gb G.Skill @ 1000mhz 2.1v 
Hard DriveOptical DriveOSMonitor
2x7200.12 500gb RAID, 2xWD5000AAKS Asus DRW-1814BLT SATA CLFSx64, W7x64 SCEPTRE 22" 1680x1050 
KeyboardPowerCaseMouse
Logitech Media Corsair HX520 Modular Antec P182 Gunmetal Logitech G7 Laser Cordless 
Mouse Pad
none 
  hide details  
Reply
post #7 of 8
Thread Starter 
Oh, yes! strcmp()! How could I forget?! Thank you so much. I've got it working now.

Thanks for pointing out the character literal thing, too. Useful to know. Thanks for your help. +Rep.
Project 4
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 Lynnfield @ 4.4 GHz, 1.41v EVGA P55 SLi (132-LF-E655-KR) - A72 BIOS EVGA GeForce GTX 680 4GB G.Skill Ripjaws @ 9-9-9-24, 1.51V 
Hard DriveOptical DriveOSMonitor
80GB Intel X25-M SSD + 500GB WD + 2TB Samsung F3 Samsung Super Writemaster DVD+-R Windows 7 Ultimate Edition Samsung SyncMaster 2433 24" 
KeyboardPowerCaseMouse
Logitech G15 Corsair TX750 750W Corsair Obsidian 800D + Scythe Kaze Master Ace Razer Deathadder Respawn - Black 
Mouse Pad
Steelseries QcK Medium - Black 
  hide details  
Reply
Project 4
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 Lynnfield @ 4.4 GHz, 1.41v EVGA P55 SLi (132-LF-E655-KR) - A72 BIOS EVGA GeForce GTX 680 4GB G.Skill Ripjaws @ 9-9-9-24, 1.51V 
Hard DriveOptical DriveOSMonitor
80GB Intel X25-M SSD + 500GB WD + 2TB Samsung F3 Samsung Super Writemaster DVD+-R Windows 7 Ultimate Edition Samsung SyncMaster 2433 24" 
KeyboardPowerCaseMouse
Logitech G15 Corsair TX750 750W Corsair Obsidian 800D + Scythe Kaze Master Ace Razer Deathadder Respawn - Black 
Mouse Pad
Steelseries QcK Medium - Black 
  hide details  
Reply
post #8 of 8
edit: guess you got it working...nvm then.
Edited by surfbumb - 8/1/11 at 9:49pm
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › C++ - Single-line input validation