Absolute beginner

Hello gods of C++;

I am an absolute begginer to programming and I am taking a self help course at home. I'm using the book teach yourself c++ in 21 days < ha ha i'm at day 10 and it has already been a month.> So far i'm comfortable with my compiler and can code from the book easily. I am having problems with the creation of my own freestyle programs.

To start: I am writing a program that will print an output to the console screen a number of times when asked. I want the program to take inputtted characters and then print out the number of times requested. So far I can do this from the main body but not from keyboard input. What am I missing?

Please help.

Here is the code:
// Priactice program.cpp : Defines the entry point for the console application.
//This is a practice program

#include "stdafx.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
cout << "This is a practice program" << endl << endl;
int Myname = 0;
int Maxname;
cout << "How many times to write your name?: " ;
cin >> Maxname;
for (;;)
{
if ( Myname < Maxname)
{
cout << "michael" << endl;
Myname++;
}
else
break;
}
char response;
cin >> response;
return 0;
}

Last edited on
Okay, Okay, OOOOKay!!!

I figured out what I was doing wrong. But there are some details that I need to have cleared up. For instance, what if I needed to include spaces between characters?

I fixed this simple program by introducing <char> and initialized it to <Nam> and gave those parameters an argument set to [100] and it worked!!!! but when I introduced spaces in my name the program just falls through.

I need further insight. Please help!!!

Here is the new code;

// Priactice program.cpp : Defines the entry point for the console application.
//This is a practice program

#include "stdafx.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
cout << "This is a practice program" << endl << endl;
int Myname = 0;
int Maxname;
char Nam [100];
cout << "Enter your name: ";
cin >> Nam;
cout << "How many times to write your name?: " ;
cin >> Maxname;
for (;;)
{
if ( Myname < Maxname)
{
cout << Nam << endl;
Myname++;
}
else
break;
}
char response;
cin >> response;
return 0;
}

Last edited on
What is the publication date on your book? Your code seems extremely dated.
The name of the book is -Sam's teach yourself c++ in 21 Days-
by Jesse Liberty and Bradley Jones
5th ed. Copyright 2005
I remeber this book. My edition was 2001.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <limits>
using namespace std;

int main()
{
   cout << "Practice program";
   int Maxname;
   string Nam;
   cout << "Enter your name: ";
   cin >> Nam;
   cout << "How many times to write your name?: ";
   cin >> MaxName;
   for(int i = 0; i < MaxName; i++)
   {
       cout << Nam << endl;
   }

   cout << "Press ENTER to continue...";
   cin.ignore(numeric_limits<std::streamsize>::max(), '\n' );
   //or system("pause") if you want to take the easy way out
  return 0;
}


The program above is the PROPER way(correct me if im wrong) to write a program like that. The book seems to teach some awful concepts, i would burn that book if i were you.

Here's a guy that has some great tutorials on c++, easy to understand and a hell of a lot more up to date than what you're reading.
http://thenewboston.org/list.php?cat=16
Good morning gods and guru's of c++

I appreciate the help and insight that you provided for me and I'm taking it all into consideration. This is my first attempt at programming so I don't want to make any comments because I need your experience to guide me but, I do want to keep the book because it is giving me the <fundamentals> that I need.

Your code is a lot more efficent than mine. the fully written for Statement cleaned up a lot of whitespace and the system pause worked perfectly. There were some minor tweaks I made to make the code work and it performed perfectly!!!!

Thank you!!!!

This is the beginning for me in this communtiy. Please work with me as I grow, I know that I can surely be a benefit here.

Here is the code with my minor tweak:


#include <iostream>
//#include <string>
//#include <limits>
using namespace std;

int main()
{
   cout << "Practice program" << endl << endl;
   int Maxname;
   char Nam [100];
   cout << "Enter your name: ";
   cin >> Nam;
   cout << "How many times to write your name?: ";
   cin >> Maxname;
   for(int i = 0; i < Maxname; i++)
   {
       cout << Nam << endl;
   }
   cout "\n\n";
   //cout << "Press ENTER to continue...";
   //cin.ignore(numeric_limits<std::streamsize>::max(), '\n' );
  system("pause") ;
  return 0;
}
Last edited on
No worries, but on one note:
1
2
//cout << "Press ENTER to continue...";
   //cin.ignore(numeric_limits<std::streamsize>::max(), '\n' ); 


Since you commented this out, the library <limits> has no use, so feel free to leave that out. system("pause") ; is a more user friendly approach to pausing at the end of a program, but beware, it is extremely inefficient and has massive security holes in larger programs. Feel free to use it in early programs, but as you progress try and detach yourself from using it.
Once again:

I would like to express my thanks for your expertise and adivse. In my studies I will be moving into inheritence and arrays in the coming few days < or weeks > I am taking my time to fully digest this language before looking into more advanced books on c++ to develop my own programming style.

Keep an eye out for me, and I'll keep reading the forums.




I've just begun learning C++ as well using a different Sam's Teach Yourself book. Your program idea looked like good practice, so I tried creating the program and came up with the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Objective: Create program that asks for user's name and a number. Display name that many times

#include <iostream>
#include <string>
using namespace std;

int main()
{
	string userName;
	int userRepeat = 0;

	cout << "Enter your name: ";
//	cin >> userName;	Error: Terminates string at first space. Need to capture full string.
	getline(cin, userName);

	cout << "Enter number of times name should be displayed: ";
	cin >> userRepeat;

	if(userRepeat > 0)
	{
		do
		{
			cout << userName << endl;
			--userRepeat;
		}
		while(userRepeat > 0);

	}
	else
	{
		cout << "Number entered was less than 1. Name not displayed. \n";
	}
		
	return 0;
}


I ran in to the same problem you described, where cin >> userName terminated at the first space and disrupt the program flow.

Following the book's examples, I used getline(cin, userName); to capture the full string. Others can correct me if I'm wrong, but that should be a more robust solution using the standard string library. Should help avoid potential crashes if your user enters a name longer than 100 characters.

A for loop probably would have been a better implementation. Will keep that in mind for next time. Here's to learning more C++!
Topic archived. No new replies allowed.