473,669 Members | 2,453 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Loop Issues

Kindle Life 123
7 New Member
My loop keeps on repeating inspite of typing N=1.

here is the code..

void club::enterdata () {
std::cout<<"Ent er details";
std::cin>>mno>> memname>>activi ty;

this is the function... and this is the funcion in which this function is called..

void club::writeinto ()
{
club c{};
int N, i = 0;
std::ofstream fout( "project.da t", std::ios::binar y );
if( ! fout )
{
std::cout<<"Err or";
}
while(!fout.eof ())
{
std::cout << "Enter the number of records to be entered";
std::cin >> N;
for(i=0;i<=N;i+ +) {
c.enterdata();
}
fout.write( (char*)&c, sizeof(c) );
}
fout.close();
}

here is the output..

Enter the number of records to be entered 1
Enter details 12 hka abc
Enter details

Pls help me out..
Sep 28 '18 #1
1 1123
weaknessforcats
9,208 Recognized Expert Moderator Expert
On the first of the loop i is 0 and N is 1, therefore i<= N is true.

Therefore, enter the loop.


Now i is 1 and N is 1, therefore i<=N is true.


Therefore, enter the loop.


Now i is 2 and N is 1, therefore i<=N is false.


Therefore, exit the loop.


Avoid the extra loop cycle by using i != N rather than i <= N.


BTW: c.enterdata() inside club::writeinto () won't compile. The address of the current object is kept in the this pointer. To use the current object inside a member function you code:


this->enterdata();
Sep 28 '18 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

7
1684
by: pescott | last post by:
I am still struggling to get some files uploaded to a database as BLOB data. I have 5 includes which allows the user to upload 5 files, numbered accordingly. However, there are script errors. Duhh, why else would I post? Well, here goes: CODE $adres = trim (htmlspecialchars($_POST)); $verkocht = trim (htmlspecialchars($_POST)); $form_description1 = trim (htmlspecialchars($_POST));
0
2773
by: Sue Adams | last post by:
I actually have two issues/questions: I have an autonumber field in an access db table that I grab and later use to update a record in another table withing the same db. The code I use to get it from the db table is: ''Retrieve the Registration Identification Number strRegisterID = Rs("Register_ID") Prior to testing my code and actually updating the db, I''m trying to write it to the page to make sure their isn''t a loop or massive...
8
1666
by: Drew | last post by:
I am building an application for keeping track of user permissions here at work. I have built the interfaces, and am now working on the processing page for inserting to the database. I am having a problem with a conditional in my loop not working correctly, I am sure that I have been overlooking something, but cannot pin it down. I have pared the code down quite a bit just so that I could resolve my issues with it. I have a page...
22
2216
by: Jan Richter | last post by:
Hi there, the Code below shows DJBs own implementation of strlen (str_len): unsigned int str_len(char *s) { register char *t; t = s; for (;;) { if (!*t) return t - s; ++t;
8
7586
by: Shamrokk | last post by:
My application has a loop that needs to run every 2 seconds or so. To acomplish this I used... "Thread.Sleep(2000);" When I run the program it runs fine. Once I press the button that starts the looping function the window becomes unmovable and cannot close under its own direction (the upper right "close 'X'") My first attempt to solve the problem was to have the looping function execute as its own thread, the idea being this would...
63
3153
by: Aaron Ackerman | last post by:
What is the sytax for exiting a for loop in C#?
8
1685
by: Microsoft | last post by:
I have the following loop the length contains somewhere around 1000+ items: For elemIndex = 0 To len - 1 elem = CType(doc.all.item(elemIndex), mshtml.IHTMLElement) If elem.tagName = "A" Then If elem.innerText = "1." Then elem.click() End If End If Next elemIndex
3
1265
by: Blckbx | last post by:
I have 27 textboxes and am wishing to cycle through them using a do... loop. this code doesn't work: N = 1 Do textbox(N).text = someStringArray(N-1) N = N + 1 loop until N = 27 brings up a compile error: sub or function not defined
13
3244
by: Rick | last post by:
The following code will enter an infinate loop when in ReadChars. I can only make it happen when reading a Stream and with this particular XML. If I use the ReadInnerXml call rather than my own ReadElementBodyAsXml the code works, but is less efficent. ReadElementBodyAsXml is required by my application with .Net Framework 1.1. The code breaks on the second call to ReadElementBodyAsXml with the inner xml: </EGDConfigExtension>...
0
8384
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8810
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8590
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7410
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6211
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4208
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2798
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2035
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1790
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.