How do I get the student names in all lowercase chars except the first letter which is capitalized?

#include <fstream>
#include <stdlib.h>
#include <time.h>
using namespace std;

int main() {
//  Step 1: Declare
	ofstream OutFile;
	int NumStudents;
	int NumChar;
	int NewScore;
	char NewChar;
//  Step 2: Initialize
	OutFile.open("C:/Scores.dat");
	srand((unsigned) time(NULL));
//  Step 3: Solve
	// Write number of weights and the weights
	OutFile << 3 << ' ' << 50 << ' ' << 40 << ' ' << 10 << endl;
	// Determine the number of students
	NumStudents = 10 + rand() % 10;
	// For each student write the last name and the scores for the name
	for (int I = 0; I < NumStudents; I++) {
		// Write the name
		NumChar = 5 + rand () % 5;
		for (int J = 0; J < NumChar; J++) {
			NewChar = 65 + rand() % 26;
			OutFile << NewChar;
		}
		OutFile << ' ';
		// Write scores
		for (J = 0; J < 3; J++) {
			NewScore = 50 + rand () % 51;
			OutFile << NewChar;
		}
		OutFile << ' ';
		// Write the scores
		for (J = 0; J < 3; J++) {
			NewScore = 50 + rand () % 51;
			OutFile << NewScore << ' ';
		}
		OutFile << endl;
	}
	// Step 4: Wrapup
	OutFile.close();
	return 0;
}

Dani AI

Generated

Short answer: build the name as a string of lowercase letters, then uppercase the first character with std::toupper. That keeps the logic clear and avoids scattering per-character arithmetic through your output code. was pointing at the right idea (loop + ASCII math) and gave a working per-character approach; a clearer, safer pattern is to fill a std::string, make sure the whole name is lowercase, then capitalise the first char.

Example pattern (different from the code already posted):

#include <random>
#include <string>
#include <cctype>

std::mt19937 rng{std::random_device{}()};
std::uniform_int_distribution<int> dist(0, 25);

std::string name;
name.resize(numChars);
for (size_t i = 0; i < name.size(); ++i)
    name[i] = char('a' + dist(rng));

name[0] = static_cast<char>(std::toupper(static_cast<unsigned char>(name[0])));
OutFile << name << ' ';

Notes and troubleshooting:

  • Use the unsigned char cast when calling std::toupper/std::tolower to avoid undefined behaviour for negative char values.
  • Prefer <random> (mt19937 + uniform_int_distribution) over rand() for clearer and better-quality randomness.
  • In the original snippet there is a bug: one loop writes NewChar instead of the intended NewScore — change that output to the numeric value.
  • If names come from an external file instead of being generated, convert the whole string to lowercase with std::transform + std::tolower, then uppercase name[0]. This keeps behaviour consistent and locale-aware if you decide to handle locales later.

Recommended Answers

All 3 Replies

Use Loop to deal with names , and Ascii to convert the first letter to capital one

ooooh... what?? :?:

If you were reading names from a file and wanted to make them prettier you could use strlwr() and the like, but here you are manufacturing totally random names in a loop, so you could do something like this (modifying your loop):

for (int J = 0; J < NumChar; J++) {
    if (J == 0)
        NewChar = 'A';
    else
        NewChar = 'a';
    NewChar += rand() % 26;
    OutFile << NewChar;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.